Tanmay09516 commited on
Commit
0d02cc4
·
verified ·
1 Parent(s): 2f0f369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -3
app.py CHANGED
@@ -7,6 +7,10 @@ from qdrant_search import QdrantSearch
7
  from langchain_groq import ChatGroq
8
  from nomic_embeddings import EmbeddingsModel
9
 
 
 
 
 
10
  # Load environment variables
11
  load_dotenv()
12
 
@@ -80,6 +84,40 @@ def chat_function(question: str):
80
 
81
  return answer.content.strip(), sources
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  # Create Gradio Interface
84
  with gr.Blocks() as demo:
85
  gr.Markdown("# 🗨️ LangAssist Chat")
@@ -107,9 +145,13 @@ with gr.Blocks() as demo:
107
  ---
108
  ## 📡 API Endpoint
109
 
110
- Since we're running on Hugging Face Spaces, direct API access is not available. Please use the interactive interface above to ask your questions.
111
  """)
112
 
113
- # Launch the Gradio App
 
 
 
114
  if __name__ == "__main__":
115
- demo.launch()
 
 
7
  from langchain_groq import ChatGroq
8
  from nomic_embeddings import EmbeddingsModel
9
 
10
+ from fastapi import FastAPI, HTTPException
11
+ from fastapi.middleware.cors import CORSMiddleware
12
+ from pydantic import BaseModel
13
+
14
  # Load environment variables
15
  load_dotenv()
16
 
 
84
 
85
  return answer.content.strip(), sources
86
 
87
+ # Define Pydantic model for request
88
+ class ChatRequest(BaseModel):
89
+ question: str
90
+
91
+ # Initialize FastAPI app
92
+ app = FastAPI()
93
+
94
+ # Define allowed origins
95
+ origins = [
96
+ "*", # Allow all origins; for production, specify your frontend domains
97
+ # Example:
98
+ # "http://localhost",
99
+ # "http://localhost:3000",
100
+ # "https://your-frontend-domain.com",
101
+ ]
102
+
103
+ # Add CORS middleware
104
+ app.add_middleware(
105
+ CORSMiddleware,
106
+ allow_origins=origins, # Allows all origins. Replace "*" with specific domains in production.
107
+ allow_credentials=True,
108
+ allow_methods=["*"], # Allows all HTTP methods.
109
+ allow_headers=["*"], # Allows all headers.
110
+ )
111
+
112
+ # Define API endpoint
113
+ @app.post("/api/chat")
114
+ async def api_chat(request: ChatRequest):
115
+ try:
116
+ answer, sources = chat_function(request.question)
117
+ return {"answer": answer, "sources": sources}
118
+ except Exception as e:
119
+ raise HTTPException(status_code=500, detail=str(e))
120
+
121
  # Create Gradio Interface
122
  with gr.Blocks() as demo:
123
  gr.Markdown("# 🗨️ LangAssist Chat")
 
145
  ---
146
  ## 📡 API Endpoint
147
 
148
+ You can access the API endpoint at `/api/chat`. For example, send a POST request to `http://localhost:8000/api/chat` with JSON body `{"question": "Your question here"}`.
149
  """)
150
 
151
+ # Mount Gradio app on FastAPI
152
+ app = gr.mount_gradio_app(app, demo, path="/gradio")
153
+
154
+ # To run, use: uvicorn app:app --host 0.0.0.0 --port 8000
155
  if __name__ == "__main__":
156
+ import uvicorn
157
+ uvicorn.run(app, host="0.0.0.0", port=8000)