Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,11 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
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.",
|
64 |
-
examples=[
|
65 |
-
["What role does T-cell count play in severe human adenovirus type 55 (HAdV-55) infection?"],
|
66 |
-
["In what school district is Governor John R. Rogers High School located?"],
|
67 |
-
["Is there a functional neural correlate of individual differences in cardiovascular reactivity?"]
|
68 |
-
]
|
69 |
)
|
70 |
-
|
71 |
-
# Launch with debugging enabled
|
72 |
-
if __name__ == "__main__":
|
73 |
-
demo.launch(debug=True)
|
|
|
1 |
+
# Initialize models and configurations
|
2 |
+
model_name = 'intfloat/e5-small'
|
3 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
4 |
+
embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
5 |
+
embedding_model.client.to(device)
|
6 |
+
|
7 |
+
# Initialize Chroma with existing database
|
8 |
+
vectordb = Chroma(
|
9 |
+
persist_directory='./docs/chroma/',
|
10 |
+
embedding_function=embedding_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
)
|
|
|
|
|
|
|
|