Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,45 +3,61 @@ from langchain.embeddings import HuggingFaceEmbeddings
|
|
3 |
from langchain_community.vectorstores import Chroma
|
4 |
import openai
|
5 |
import torch
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
11 |
-
embedding_model.client.to(device)
|
12 |
|
13 |
-
# Initialize
|
14 |
-
|
15 |
-
persist_directory='./docs/chroma/',
|
16 |
-
embedding_function=embedding_model
|
17 |
-
)
|
18 |
|
19 |
def process_query(query):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
#
|
38 |
demo = gr.Interface(
|
39 |
fn=process_query,
|
40 |
inputs=[
|
41 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
42 |
],
|
43 |
outputs=[
|
44 |
-
gr.Textbox(
|
|
|
|
|
|
|
45 |
],
|
46 |
title="RAG-Powered Question Answering System",
|
47 |
description="Ask questions and get answers based on the embedded document knowledge.",
|
@@ -52,6 +68,6 @@ demo = gr.Interface(
|
|
52 |
]
|
53 |
)
|
54 |
|
55 |
-
# Launch
|
56 |
if __name__ == "__main__":
|
57 |
-
demo.launch()
|
|
|
3 |
from langchain_community.vectorstores import Chroma
|
4 |
import openai
|
5 |
import torch
|
6 |
+
import logging
|
7 |
|
8 |
+
# Set up logging
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
11 |
|
12 |
+
# Initialize OpenAI API key
|
13 |
+
openai.api_key = 'YOUR_API_KEY' # Replace with your API key
|
|
|
|
|
|
|
14 |
|
15 |
def process_query(query):
|
16 |
+
try:
|
17 |
+
# Log query processing
|
18 |
+
logger.info(f"Processing query: {query}")
|
19 |
+
|
20 |
+
# Get relevant documents
|
21 |
+
relevant_docs = vectordb.similarity_search(query, k=30)
|
22 |
+
context = " ".join([doc.page_content for doc in relevant_docs])
|
23 |
+
|
24 |
+
# Add delay to respect API rate limits
|
25 |
+
time.sleep(1)
|
26 |
+
|
27 |
+
# Generate response using OpenAI
|
28 |
+
response = openai.chat.completions.create(
|
29 |
+
model="gpt-4",
|
30 |
+
messages=[
|
31 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
32 |
+
{"role": "user", "content": f"Given the document: {context}\n\nGenerate a response to the query: {query}"}
|
33 |
+
],
|
34 |
+
max_tokens=300,
|
35 |
+
temperature=0.7,
|
36 |
+
)
|
37 |
+
|
38 |
+
answer = response.choices[0].message.content.strip()
|
39 |
+
logger.info("Successfully generated response")
|
40 |
+
return answer
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
logger.error(f"Error processing query: {str(e)}")
|
44 |
+
return f"Here's what went wrong: {str(e)}"
|
45 |
|
46 |
+
# Enhanced Gradio interface
|
47 |
demo = gr.Interface(
|
48 |
fn=process_query,
|
49 |
inputs=[
|
50 |
+
gr.Textbox(
|
51 |
+
label="Enter your question",
|
52 |
+
placeholder="Type your question here...",
|
53 |
+
lines=2
|
54 |
+
)
|
55 |
],
|
56 |
outputs=[
|
57 |
+
gr.Textbox(
|
58 |
+
label="Answer",
|
59 |
+
lines=5
|
60 |
+
)
|
61 |
],
|
62 |
title="RAG-Powered Question Answering System",
|
63 |
description="Ask questions and get answers based on the embedded document knowledge.",
|
|
|
68 |
]
|
69 |
)
|
70 |
|
71 |
+
# Launch with debugging enabled
|
72 |
if __name__ == "__main__":
|
73 |
+
demo.launch(debug=True)
|