Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .env +1 -0
- Dockerfile +9 -0
- app.py +28 -0
- requirements.txt +3 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GEMINI_API_KEY='AIzaSyBM-QVojJCqw91OS6Brt1aE_pPsw-UKe8c'
|
Dockerfile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
COPY requirements.txt .
|
| 5 |
+
RUN pip install -r requirements.txt
|
| 6 |
+
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 8 |
+
GEMINI_ENDPOINT = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={GEMINI_API_KEY}"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@app.post("/api/ai-insight")
|
| 12 |
+
async def generate_insight(request: Request):
|
| 13 |
+
body = await request.json()
|
| 14 |
+
prompt = body.get("prompt", "Analyze this chart data.")
|
| 15 |
+
|
| 16 |
+
response = requests.post(
|
| 17 |
+
GEMINI_ENDPOINT,
|
| 18 |
+
headers={"Content-Type": "application/json"},
|
| 19 |
+
json={"contents": [{"parts": [{"text": prompt}]}]},
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
data = response.json()
|
| 23 |
+
try:
|
| 24 |
+
return {
|
| 25 |
+
"insight": data["candidates"][0]["content"]["parts"][0]["text"]
|
| 26 |
+
}
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return {"error": "Gemini response parsing failed", "details": str(e)}
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
requests
|