Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
""
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import requests
|
| 4 |
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Initialize OpenAI client
|
| 7 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 8 |
+
|
| 9 |
+
# Vector database search function
|
| 10 |
+
def search_document(query, k=5):
|
| 11 |
+
url = "http://154.12.226.68:8000/search"
|
| 12 |
+
|
| 13 |
+
payload = {
|
| 14 |
+
"text": query,
|
| 15 |
+
"k": k
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
headers = {
|
| 19 |
+
"Content-Type": "application/json"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 24 |
+
response.raise_for_status()
|
| 25 |
+
return response.json()
|
| 26 |
+
except requests.exceptions.RequestException as e:
|
| 27 |
+
return f"An error occurred: {e}"
|
| 28 |
+
|
| 29 |
+
# Function to query OpenAI
|
| 30 |
+
def query_openai(prompt):
|
| 31 |
+
try:
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="gpt-4o-mini",
|
| 34 |
+
messages=[
|
| 35 |
+
{"role": "system", "content": "You are a helpful assistant. Answer the question based on the provided context."},
|
| 36 |
+
{"role": "user", "content": prompt}
|
| 37 |
+
]
|
| 38 |
+
)
|
| 39 |
+
return response.choices[0].message.content
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"An error occurred while querying OpenAI: {e}"
|
| 42 |
+
|
| 43 |
+
# Function to perform vector search and format results
|
| 44 |
+
def vector_search(query):
|
| 45 |
+
results = search_document(query)
|
| 46 |
+
if isinstance(results, str): # Error occurred
|
| 47 |
+
return results
|
| 48 |
+
if not isinstance(results, dict) or 'results' not in results:
|
| 49 |
+
return "Unexpected format in vector database response."
|
| 50 |
+
|
| 51 |
+
formatted_results = ""
|
| 52 |
+
for i, result in enumerate(results['results'], 1):
|
| 53 |
+
content = result['metadata']['content']
|
| 54 |
+
source = f"Source {i}: {result['metadata'].get('source', 'Unknown source')}, page {result['metadata'].get('page', 'Unknown page')}"
|
| 55 |
+
metadata = ", ".join([f"{k}: {v}" for k, v in result['metadata'].items() if k != 'content'])
|
| 56 |
+
formatted_results += f"{source}\nMetadata: {metadata}\nContent: {content}\n\n"
|
| 57 |
+
|
| 58 |
+
return formatted_results
|
| 59 |
+
|
| 60 |
+
# Combined function for search and query
|
| 61 |
+
def search_and_query(question):
|
| 62 |
+
# First, perform the vector search
|
| 63 |
+
search_results = vector_search(question)
|
| 64 |
+
|
| 65 |
+
# Then, use these results to query OpenAI
|
| 66 |
+
prompt = f"""Given the following context and question, provide a comprehensive and accurate answer. Use ONLY the information provided in the context to answer. If the context doesn't contain relevant information to answer the question, state that clearly.
|
| 67 |
+
|
| 68 |
+
Context:
|
| 69 |
+
{search_results}
|
| 70 |
+
|
| 71 |
+
Question: {question}
|
| 72 |
+
|
| 73 |
+
Answer:"""
|
| 74 |
+
|
| 75 |
+
openai_response = query_openai(prompt)
|
| 76 |
+
|
| 77 |
+
# Return both the search results and the OpenAI response
|
| 78 |
+
return search_results, openai_response
|
| 79 |
+
|
| 80 |
+
# Gradio interface
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
question_input = gr.Textbox(label="Enter your question")
|
| 83 |
+
search_output = gr.Textbox(label="Vector Search Results")
|
| 84 |
+
answer_output = gr.Textbox(label="OpenAI Answer")
|
| 85 |
+
query_button = gr.Button("Get Answer")
|
| 86 |
+
|
| 87 |
+
query_button.click(search_and_query, inputs=question_input, outputs=[search_output, answer_output])
|
| 88 |
+
|
| 89 |
+
demo.launch()
|