Spaces:
Sleeping
Sleeping
Subhakanta
commited on
Commit
·
f75fd18
1
Parent(s):
70e82ee
changing app file
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.
|
| 5 |
-
from fastapi.responses import FileResponse
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from src.chatbot import handle_query
|
|
|
|
| 8 |
|
| 9 |
class ChatRequest(BaseModel):
|
| 10 |
query: str
|
|
@@ -24,13 +24,53 @@ app.add_middleware(
|
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
@app.get("/")
|
| 31 |
-
def root():
|
| 32 |
"""Serve the frontend HTML"""
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
@app.post("/chat", response_model=ChatResponse)
|
| 36 |
def chat(request: ChatRequest):
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.responses import FileResponse, Response
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from src.chatbot import handle_query
|
| 7 |
+
from pathlib import Path
|
| 8 |
|
| 9 |
class ChatRequest(BaseModel):
|
| 10 |
query: str
|
|
|
|
| 24 |
allow_headers=["*"],
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# Get the directory where app.py is located
|
| 28 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 29 |
+
frontend_path = BASE_DIR / "frontend"
|
| 30 |
|
| 31 |
@app.get("/")
|
| 32 |
+
async def root():
|
| 33 |
"""Serve the frontend HTML"""
|
| 34 |
+
html_file = frontend_path / "index.html"
|
| 35 |
+
if html_file.exists():
|
| 36 |
+
return FileResponse(html_file)
|
| 37 |
+
return {"error": "index.html not found", "path": str(html_file)}
|
| 38 |
+
|
| 39 |
+
@app.get("/style.css")
|
| 40 |
+
async def get_css():
|
| 41 |
+
"""Serve CSS file"""
|
| 42 |
+
css_file = frontend_path / "style.css"
|
| 43 |
+
if css_file.exists():
|
| 44 |
+
return FileResponse(css_file, media_type="text/css")
|
| 45 |
+
return {"error": "style.css not found"}
|
| 46 |
+
|
| 47 |
+
@app.get("/script.js")
|
| 48 |
+
async def get_js():
|
| 49 |
+
"""Serve JavaScript file with automatic API URL fix"""
|
| 50 |
+
js_file = frontend_path / "script.js"
|
| 51 |
+
if js_file.exists():
|
| 52 |
+
# Read the original file
|
| 53 |
+
with open(js_file, 'r', encoding='utf-8') as f:
|
| 54 |
+
content = f.read()
|
| 55 |
+
|
| 56 |
+
# Replace localhost URL with empty string for same-origin requests
|
| 57 |
+
content = content.replace(
|
| 58 |
+
'const API_BASE_URL = "http://localhost:8000";',
|
| 59 |
+
'const API_BASE_URL = "";'
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Return the modified JavaScript
|
| 63 |
+
return Response(content=content, media_type="application/javascript")
|
| 64 |
+
return {"error": "script.js not found"}
|
| 65 |
+
|
| 66 |
+
@app.get("/logo.png")
|
| 67 |
+
async def get_logo():
|
| 68 |
+
"""Serve logo image"""
|
| 69 |
+
logo_file = frontend_path / "logo.png"
|
| 70 |
+
if logo_file.exists():
|
| 71 |
+
return FileResponse(logo_file, media_type="image/png")
|
| 72 |
+
# Return a placeholder or error
|
| 73 |
+
return {"error": "logo.png not found"}
|
| 74 |
|
| 75 |
@app.post("/chat", response_model=ChatResponse)
|
| 76 |
def chat(request: ChatRequest):
|