Download app.py from Birdboy89/ai: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/spaces/Birdboy89/ai/resolve/main/app.py
- Command line
-
hf download hf://spaces/Birdboy89/ai/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Birdboy89/ai/resolve/main/app.py
2.76 kB
| import openai | |
| # NEW: from openai import OpenAI | |
| from llama_index import ( | |
| SimpleDirectoryReader, GPTVectorStoreIndex, LLMPredictor, PromptHelper, | |
| VectorStoreIndex, load_index_from_storage, StorageContext, Prompt | |
| ) | |
| from llama_index.prompts import ChatPromptTemplate, ChatMessage, MessageRole | |
| from langchain.chat_models import ChatOpenAI | |
| import gradio as gr | |
| import logging | |
| import os | |
| # OpenAI client initialization | |
| # client = OpenAI(api_key=os.environ['OPENAI_API_KEY']) | |
| api_key=os.environ['OPENAI_API_KEY'] | |
| # Set up logging configuration | |
| logging.basicConfig(filename='Questions_asked.log', level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') | |
| def construct_index(directory_path): | |
| max_input_size = 4896 | |
| num_outputs = 512 | |
| max_chunk_overlap = 20 | |
| chunk_size_limit = 681 | |
| prompt_helper = PromptHelper(max_input_size, num_outputs, chunk_overlap_ratio=0.1, chunk_size_limit=chunk_size_limit) | |
| llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs)) | |
| documents = SimpleDirectoryReader(directory_path).load_data() | |
| # INDEX | |
| index = GPTVectorStoreIndex.from_documents(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper) | |
| # SAVE | |
| index.set_index_id("vector_index") | |
| index.storage_context.persist("./storage") | |
| return index | |
| def chatbot(input_text): | |
| # rebuild storage context | |
| storage_context = StorageContext.from_defaults(persist_dir="storage") | |
| # load index | |
| index = load_index_from_storage(storage_context, index_id="vector_index") | |
| # Create response template | |
| TEMPLATE_STR=( | |
| "I am a bot specialized in answering questions related to SATS. Please ask your question in any language, and I will respond in the same language. For more precise answers, include specific details in your query.\n" | |
| "---------------------\n" | |
| "{context_str}" | |
| "\n---------------------\n" | |
| "Given the context provided above, I will now address your question: {query_str}. For yes/no questions, I will offer further explanation or guidance. If the answer is beyond my capabilities, I will inform you." | |
| ) | |
| QA_TEMPLATE = Prompt(TEMPLATE_STR) | |
| query_engine = index.as_query_engine(text_qa_template=QA_TEMPLATE, response_mode="compact") | |
| response = query_engine.query(input_text) | |
| return response.response | |
| # Construct the index with documents directory | |
| index = construct_index("docs") | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=chatbot, | |
| inputs=gr.components.Textbox(lines=7, label="Ask with a spesific case or about rules on a subject"), | |
| outputs="text", | |
| title="SATS AI" | |
| ) | |
| iface.launch(share=False, auth=(os.environ.get("User"), os.environ.get("Password"))) | |