Spaces:
Sleeping
Sleeping
Rename src/streamlit_app.py to app.py
Browse files- app.py +61 -0
- src/streamlit_app.py +0 -40
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from google import genai
|
| 5 |
+
from google.genai import types
|
| 6 |
+
|
| 7 |
+
# --------------------
|
| 8 |
+
# App setup
|
| 9 |
+
# --------------------
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# --------------------
|
| 20 |
+
# Gemini client
|
| 21 |
+
# --------------------
|
| 22 |
+
# HF → Settings → Variables and secrets
|
| 23 |
+
# Name: GEMINI_API_KEY
|
| 24 |
+
client = genai.Client() # auto-picks GEMINI_API_KEY
|
| 25 |
+
|
| 26 |
+
DEFAULT_MODEL = "gemini-2.5-flash"
|
| 27 |
+
|
| 28 |
+
# --------------------
|
| 29 |
+
# Routes
|
| 30 |
+
# --------------------
|
| 31 |
+
@app.post("/api/generate")
|
| 32 |
+
async def generate(req: Request):
|
| 33 |
+
try:
|
| 34 |
+
payload = await req.json()
|
| 35 |
+
|
| 36 |
+
model = payload.get("model", DEFAULT_MODEL)
|
| 37 |
+
config = payload.get("config", None)
|
| 38 |
+
|
| 39 |
+
# Simple mode
|
| 40 |
+
if "prompt" in payload:
|
| 41 |
+
contents = payload["prompt"]
|
| 42 |
+
# Full SDK-compatible payload
|
| 43 |
+
elif "contents" in payload:
|
| 44 |
+
contents = payload["contents"]
|
| 45 |
+
else:
|
| 46 |
+
raise HTTPException(
|
| 47 |
+
status_code=400,
|
| 48 |
+
detail="Expected `prompt` or `contents`",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
response = client.models.generate_content(
|
| 52 |
+
model=model,
|
| 53 |
+
contents=contents,
|
| 54 |
+
config=config,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Return raw structured response
|
| 58 |
+
return response.model_dump()
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=str(e))
|
src/streamlit_app.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|