Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,37 +2,20 @@
|
|
2 |
import os
|
3 |
import warnings
|
4 |
from dotenv import load_dotenv
|
5 |
-
|
6 |
-
from fastapi.middleware.cors import CORSMiddleware
|
7 |
-
from pydantic import BaseModel
|
8 |
from qdrant_search import QdrantSearch
|
9 |
from langchain_groq import ChatGroq
|
10 |
from nomic_embeddings import EmbeddingsModel
|
11 |
-
import gradio as gr
|
12 |
-
from starlette.middleware.wsgi import WSGIMiddleware
|
13 |
-
from starlette.responses import RedirectResponse
|
14 |
|
15 |
-
# Load environment variables from
|
16 |
-
|
17 |
|
18 |
# Suppress FutureWarnings
|
19 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
20 |
|
21 |
-
# Disable tokenizers parallelism
|
22 |
os.environ["TOKENIZERS_PARALLELISM"] = "FALSE"
|
23 |
|
24 |
-
# Initialize FastAPI app
|
25 |
-
app = FastAPI()
|
26 |
-
|
27 |
-
# Allow CORS for frontend on Vercel or any other frontend
|
28 |
-
app.add_middleware(
|
29 |
-
CORSMiddleware,
|
30 |
-
allow_origins=["*"], # Replace "*" with your frontend URL for better security
|
31 |
-
allow_credentials=True,
|
32 |
-
allow_methods=["*"],
|
33 |
-
allow_headers=["*"],
|
34 |
-
)
|
35 |
-
|
36 |
# Initialize global variables
|
37 |
collection_names = ["docs_v1_2", "docs_v2_2", "docs_v3_2"]
|
38 |
limit = 5
|
@@ -50,29 +33,26 @@ search = QdrantSearch(
|
|
50 |
embeddings=embeddings
|
51 |
)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
question
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
sources: list
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
query =
|
65 |
if not query:
|
66 |
-
|
67 |
|
68 |
# Step 1: Retrieve relevant documents from Qdrant
|
69 |
retrieved_docs = search.query_multiple_collections(query, collection_names, limit)
|
70 |
|
71 |
if not retrieved_docs:
|
72 |
-
return
|
73 |
-
answer="⚠️ **No relevant documents found** for your query.",
|
74 |
-
sources=[]
|
75 |
-
)
|
76 |
|
77 |
# Step 2: Prepare the context from retrieved documents
|
78 |
context = "\n\n".join([doc['text'] for doc in retrieved_docs])
|
@@ -90,43 +70,19 @@ async def chat_endpoint(request: QueryRequest):
|
|
90 |
try:
|
91 |
answer = llm.invoke(prompt)
|
92 |
except Exception as e:
|
93 |
-
|
94 |
|
95 |
# Prepare sources
|
96 |
-
|
97 |
-
{
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
# Return the answer and sources
|
104 |
-
return AnswerResponse(answer=answer.content.strip(), sources=sources)
|
105 |
-
|
106 |
-
# Gradio function to wrap around the chat endpoint
|
107 |
-
def gradio_chat(question: str):
|
108 |
-
request = QueryRequest(question=question)
|
109 |
-
try:
|
110 |
-
response = chat_endpoint(request)
|
111 |
-
if hasattr(response, '__await__'):
|
112 |
-
import asyncio
|
113 |
-
response = asyncio.run(response)
|
114 |
-
answer = response.answer
|
115 |
-
sources = response.sources
|
116 |
-
# Prepare sources for display
|
117 |
-
sources_md = "\n\n".join([
|
118 |
-
f"**Source:** {src['source']}\n**Excerpt:** {src['text']}"
|
119 |
-
for src in sources
|
120 |
-
])
|
121 |
-
return answer, sources_md
|
122 |
-
except HTTPException as http_exc:
|
123 |
-
return f"❌ **Error {http_exc.status_code}:** {http_exc.detail}", "No sources available."
|
124 |
-
except Exception as e:
|
125 |
-
return f"⚠️ **Error:** {str(e)}", "No sources available."
|
126 |
|
127 |
# Create Gradio Interface
|
128 |
-
|
129 |
-
fn=
|
130 |
inputs=gr.Textbox(
|
131 |
lines=2,
|
132 |
placeholder="Type your question here...",
|
@@ -140,16 +96,6 @@ gradio_app = gr.Interface(
|
|
140 |
description="Ask questions about the LangChain Python Library and get answers based on the latest documentation."
|
141 |
)
|
142 |
|
143 |
-
#
|
144 |
-
app.mount("/gradio", WSGIMiddleware(gradio_app))
|
145 |
-
|
146 |
-
# Redirect root to Gradio interface
|
147 |
-
@app.get("/")
|
148 |
-
async def root():
|
149 |
-
return RedirectResponse(url="/gradio")
|
150 |
-
|
151 |
-
# Only necessary when running locally
|
152 |
-
# Remove or comment out when deploying on Hugging Face
|
153 |
# if __name__ == "__main__":
|
154 |
-
#
|
155 |
-
# uvicorn.run("app:app", host="0.0.0.0", port=8000)
|
|
|
2 |
import os
|
3 |
import warnings
|
4 |
from dotenv import load_dotenv
|
5 |
+
import gradio as gr
|
|
|
|
|
6 |
from qdrant_search import QdrantSearch
|
7 |
from langchain_groq import ChatGroq
|
8 |
from nomic_embeddings import EmbeddingsModel
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load environment variables from .env file
|
11 |
+
load_dotenv()
|
12 |
|
13 |
# Suppress FutureWarnings
|
14 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
15 |
|
16 |
+
# Disable tokenizers parallelism to avoid potential issues
|
17 |
os.environ["TOKENIZERS_PARALLELISM"] = "FALSE"
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Initialize global variables
|
20 |
collection_names = ["docs_v1_2", "docs_v2_2", "docs_v3_2"]
|
21 |
limit = 5
|
|
|
33 |
embeddings=embeddings
|
34 |
)
|
35 |
|
36 |
+
def chat_endpoint(question: str):
|
37 |
+
"""
|
38 |
+
Handles the chat functionality by processing the user's question,
|
39 |
+
retrieving relevant documents, generating an answer, and returning sources.
|
40 |
|
41 |
+
Args:
|
42 |
+
question (str): The user's question.
|
|
|
43 |
|
44 |
+
Returns:
|
45 |
+
Tuple[str, str]: The generated answer and the sources used.
|
46 |
+
"""
|
47 |
+
query = question.strip()
|
48 |
if not query:
|
49 |
+
return "❌ **Error:** Query cannot be empty.", "No sources available."
|
50 |
|
51 |
# Step 1: Retrieve relevant documents from Qdrant
|
52 |
retrieved_docs = search.query_multiple_collections(query, collection_names, limit)
|
53 |
|
54 |
if not retrieved_docs:
|
55 |
+
return "⚠️ **No relevant documents found** for your query.", "No sources available."
|
|
|
|
|
|
|
56 |
|
57 |
# Step 2: Prepare the context from retrieved documents
|
58 |
context = "\n\n".join([doc['text'] for doc in retrieved_docs])
|
|
|
70 |
try:
|
71 |
answer = llm.invoke(prompt)
|
72 |
except Exception as e:
|
73 |
+
return f"⚠️ **Error generating answer:** {str(e)}", "No sources available."
|
74 |
|
75 |
# Prepare sources
|
76 |
+
sources_md = "\n\n".join([
|
77 |
+
f"**Source:** {src['source']}\n**Excerpt:** {src['text']}"
|
78 |
+
for src in retrieved_docs
|
79 |
+
])
|
80 |
+
|
81 |
+
return answer.content.strip(), sources_md
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
# Create Gradio Interface
|
84 |
+
interface = gr.Interface(
|
85 |
+
fn=chat_endpoint,
|
86 |
inputs=gr.Textbox(
|
87 |
lines=2,
|
88 |
placeholder="Type your question here...",
|
|
|
96 |
description="Ask questions about the LangChain Python Library and get answers based on the latest documentation."
|
97 |
)
|
98 |
|
99 |
+
# If running locally, uncomment the following lines:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
# if __name__ == "__main__":
|
101 |
+
# interface.launch()
|
|