Spaces:
Running
Running
Rename gemini.py to app.py
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Copy
|
2 |
+
import os
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from fastapi.responses import FileResponse, JSONResponse
|
5 |
+
from fastapi.staticfiles import StaticFiles
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Serve static files if you have any
|
10 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
11 |
+
|
12 |
+
@app.get("/")
|
13 |
+
async def read_root():
|
14 |
+
return FileResponse('index.html') # Your main HTML file
|
15 |
+
|
16 |
+
@app.get("/api/gemini-key")
|
17 |
+
async def get_gemini_key():
|
18 |
+
# Get API key from environment variable (set in Space settings)
|
19 |
+
api_key = os.environ.get("GEMINI_KEY")
|
20 |
+
if not api_key:
|
21 |
+
return JSONResponse({"error": "API key not found"}, status_code=404)
|
22 |
+
return JSONResponse({"key": api_key})
|
gemini.py
DELETED
@@ -1,29 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
-
from pydantic import BaseModel
|
3 |
-
import os
|
4 |
-
import requests
|
5 |
-
|
6 |
-
app = FastAPI()
|
7 |
-
API_KEY = os.environ.get('GEMINI_KEY') # This gets the secret
|
8 |
-
|
9 |
-
class ChatRequest(BaseModel):
|
10 |
-
message: str
|
11 |
-
context: str
|
12 |
-
|
13 |
-
@app.post("/chat")
|
14 |
-
async def chat_endpoint(request: ChatRequest):
|
15 |
-
try:
|
16 |
-
response = requests.post(
|
17 |
-
"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent",
|
18 |
-
params={"key": API_KEY},
|
19 |
-
json={
|
20 |
-
"contents": [{
|
21 |
-
"parts": [{
|
22 |
-
"text": f"Using this context: {request.context[:3000]}\nAnswer: {request.message}"
|
23 |
-
}]
|
24 |
-
}]
|
25 |
-
}
|
26 |
-
)
|
27 |
-
return response.json()
|
28 |
-
except Exception as e:
|
29 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|