Spaces:
Runtime error
Runtime error
Aetheris commited on
Commit ·
a1bfead
1
Parent(s): e7807af
add FastAPI wrapper for /api/chat endpoint
Browse files- Dockerfile +1 -1
- app.py +24 -11
Dockerfile
CHANGED
|
@@ -7,7 +7,7 @@ RUN apt-get update -qq && apt-get install -y -qq python3 python3-pip python3-ven
|
|
| 7 |
RUN python3 -m venv /venv
|
| 8 |
ENV PATH=/venv/bin:$PATH
|
| 9 |
|
| 10 |
-
RUN pip3 install -q gradio requests
|
| 11 |
|
| 12 |
WORKDIR /app
|
| 13 |
COPY app.py .
|
|
|
|
| 7 |
RUN python3 -m venv /venv
|
| 8 |
ENV PATH=/venv/bin:$PATH
|
| 9 |
|
| 10 |
+
RUN pip3 install -q gradio requests fastapi uvicorn python-multipart
|
| 11 |
|
| 12 |
WORKDIR /app
|
| 13 |
COPY app.py .
|
app.py
CHANGED
|
@@ -1,9 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests, json, os, time
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
OLLAMA = "http://localhost:11434"
|
| 5 |
MODEL = "FableForge-AI/shellwhisperer"
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ── helpers ──
|
| 8 |
|
| 9 |
def model_info():
|
|
@@ -81,7 +95,14 @@ def generate(prompt, temp):
|
|
| 81 |
|
| 82 |
with gr.Blocks(
|
| 83 |
title="ShellWhisperer-1.5B · API",
|
|
|
|
| 84 |
fill_height=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
) as demo:
|
| 86 |
|
| 87 |
gr.Markdown("""# 🐚 ShellWhisperer-1.5B · API Demo
|
|
@@ -201,15 +222,7 @@ print(r.json()["message"]["content"])
|
|
| 201 |
outputs=out,
|
| 202 |
)
|
| 203 |
|
|
|
|
|
|
|
| 204 |
if __name__ == "__main__":
|
| 205 |
-
|
| 206 |
-
server_name="0.0.0.0",
|
| 207 |
-
server_port=7860,
|
| 208 |
-
theme=gr.themes.Soft(),
|
| 209 |
-
css="""footer { display: none !important; }
|
| 210 |
-
.status-ok { color: #22c55e; font-weight: 600; }
|
| 211 |
-
.status-loading { color: #f59e0b; font-weight: 600; }
|
| 212 |
-
.status-err { color: #ef4444; font-weight: 600; }
|
| 213 |
-
.api-box { background: #1f2937; color: #e5e7eb; padding: 1em; border-radius: 8px; font-family: monospace; font-size: 0.9em; overflow-x: auto; }
|
| 214 |
-
""",
|
| 215 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests, json, os, time
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
OLLAMA = "http://localhost:11434"
|
| 8 |
MODEL = "FableForge-AI/shellwhisperer"
|
| 9 |
|
| 10 |
+
fastapi_app = FastAPI(docs_url=None, redoc_url=None)
|
| 11 |
+
|
| 12 |
+
@fastapi_app.post("/api/chat")
|
| 13 |
+
async def api_chat(request: Request):
|
| 14 |
+
body = await request.json()
|
| 15 |
+
try:
|
| 16 |
+
r = requests.post(f"{OLLAMA}/api/chat", json=body, timeout=120)
|
| 17 |
+
return JSONResponse(content=r.json())
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 20 |
+
|
| 21 |
# ── helpers ──
|
| 22 |
|
| 23 |
def model_info():
|
|
|
|
| 95 |
|
| 96 |
with gr.Blocks(
|
| 97 |
title="ShellWhisperer-1.5B · API",
|
| 98 |
+
theme=gr.themes.Soft(),
|
| 99 |
fill_height=True,
|
| 100 |
+
css="""footer { display: none !important; }
|
| 101 |
+
.status-ok { color: #22c55e; font-weight: 600; }
|
| 102 |
+
.status-loading { color: #f59e0b; font-weight: 600; }
|
| 103 |
+
.status-err { color: #ef4444; font-weight: 600; }
|
| 104 |
+
.api-box { background: #1f2937; color: #e5e7eb; padding: 1em; border-radius: 8px; font-family: monospace; font-size: 0.9em; overflow-x: auto; }
|
| 105 |
+
""",
|
| 106 |
) as demo:
|
| 107 |
|
| 108 |
gr.Markdown("""# 🐚 ShellWhisperer-1.5B · API Demo
|
|
|
|
| 222 |
outputs=out,
|
| 223 |
)
|
| 224 |
|
| 225 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|
| 226 |
+
|
| 227 |
if __name__ == "__main__":
|
| 228 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|