Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,16 @@
|
|
| 1 |
import os
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from google import genai
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 12 |
if API_KEY:
|
| 13 |
client = genai.Client(api_key=API_KEY)
|
|
@@ -17,25 +20,16 @@ else:
|
|
| 17 |
class ChatRequest(BaseModel):
|
| 18 |
message: str
|
| 19 |
|
| 20 |
-
#
|
| 21 |
@app.get("/", response_class=HTMLResponse)
|
| 22 |
-
def get_ui():
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
with open("style.css", "r", encoding="utf-8") as f:
|
| 27 |
-
css = f.read()
|
| 28 |
-
with open("script.js", "r", encoding="utf-8") as f:
|
| 29 |
-
js = f.read()
|
| 30 |
-
|
| 31 |
-
# HTML এ CSS এবং JS ইনজেক্ট করা
|
| 32 |
-
full_code = html.replace("", f"<style>{css}</style>")
|
| 33 |
-
full_code = full_code.replace("", f"<script>{js}</script>")
|
| 34 |
-
return full_code
|
| 35 |
except Exception as e:
|
| 36 |
-
return f"<h1>
|
| 37 |
|
| 38 |
-
# চ্যাট এপিআই এন্ডপয়েন্ট
|
| 39 |
@app.post("/api/chat")
|
| 40 |
async def chat_endpoint(request: ChatRequest):
|
| 41 |
if not client:
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from fastapi.templating import Jinja2Templates
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from google import genai
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# স্ট্যাটিক এবং টেমপ্লেট পাথ সেটআপ (যাতে মেমোরি লিক না হয়)
|
| 12 |
+
templates = Jinja2Templates(directory=".")
|
| 13 |
+
|
| 14 |
API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 15 |
if API_KEY:
|
| 16 |
client = genai.Client(api_key=API_KEY)
|
|
|
|
| 20 |
class ChatRequest(BaseModel):
|
| 21 |
message: str
|
| 22 |
|
| 23 |
+
# রুট ইউআরএল - মেমোরি ফ্রেন্ডলি টেমপ্লেট রেন্ডারিং
|
| 24 |
@app.get("/", response_class=HTMLResponse)
|
| 25 |
+
async def get_ui(request: Request):
|
| 26 |
try:
|
| 27 |
+
# index.html সরাসরি রেন্ডার হবে, পাইথনকে কষ্ট করে ফাইল রিড করতে হবে না
|
| 28 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
return HTMLResponse(content=f"<h1>Setup Error: {str(e)}</h1>", status_code=500)
|
| 31 |
|
| 32 |
+
# চ্যাট এপিআই এন্ডপয়েন্ট
|
| 33 |
@app.post("/api/chat")
|
| 34 |
async def chat_endpoint(request: ChatRequest):
|
| 35 |
if not client:
|