Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,52 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
with gr.Blocks(fill_height=True) as demo:
|
| 4 |
with gr.Sidebar():
|
| 5 |
gr.Markdown("# Inference Provider")
|
| 6 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
button = gr.LoginButton("Sign in")
|
|
|
|
| 8 |
gr.load("models/open-r1/OlympicCoder-7B", accept_token=button, provider="featherless-ai")
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
+
# -----------------
|
| 7 |
+
# Gradio UI
|
| 8 |
+
# -----------------
|
| 9 |
with gr.Blocks(fill_height=True) as demo:
|
| 10 |
with gr.Sidebar():
|
| 11 |
gr.Markdown("# Inference Provider")
|
| 12 |
+
gr.Markdown(
|
| 13 |
+
"This Space showcases the open-r1/OlympicCoder-7B model, "
|
| 14 |
+
"served by the featherless-ai API. Sign in with your Hugging Face "
|
| 15 |
+
"account to use this API."
|
| 16 |
+
)
|
| 17 |
button = gr.LoginButton("Sign in")
|
| 18 |
+
|
| 19 |
gr.load("models/open-r1/OlympicCoder-7B", accept_token=button, provider="featherless-ai")
|
| 20 |
+
|
| 21 |
+
# -----------------
|
| 22 |
+
# FastAPI backend for API
|
| 23 |
+
# -----------------
|
| 24 |
+
app = FastAPI()
|
| 25 |
+
|
| 26 |
+
class Query(BaseModel):
|
| 27 |
+
text: str
|
| 28 |
+
token: str = None # Optional HF token if needed
|
| 29 |
+
|
| 30 |
+
@app.post("/predict")
|
| 31 |
+
def predict(query: Query):
|
| 32 |
+
"""Call the Hugging Face inference API for the model."""
|
| 33 |
+
api_url = "https://api-inference.huggingface.co/models/open-r1/OlympicCoder-7B"
|
| 34 |
+
headers = {}
|
| 35 |
+
if query.token:
|
| 36 |
+
headers["Authorization"] = f"Bearer {query.token}"
|
| 37 |
+
|
| 38 |
+
payload = {"inputs": query.text}
|
| 39 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
| 40 |
+
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
try:
|
| 43 |
+
result = response.json()
|
| 44 |
+
return {"response": result}
|
| 45 |
+
except Exception:
|
| 46 |
+
return {"error": "Invalid JSON response from model"}
|
| 47 |
+
else:
|
| 48 |
+
return {"error": response.text}
|
| 49 |
+
|
| 50 |
+
# Mount Gradio app inside FastAPI
|
| 51 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 52 |
+
|