Spaces:
Sleeping
Sleeping
File size: 1,281 Bytes
93c0ec9 7c4cf70 8c8ff71 88df89c 39dcb26 7c4cf70 39dcb26 f03dace 8c8ff71 7c8e447 a66bba3 7c8e447 88df89c 39dcb26 88df89c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import os
import requests
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
OLLAMA_API_URL = "http://localhost:11434"
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.post("/chat_api")
async def chat_endpoint(request: Request):
body = await request.json()
model = body.get("model", "qwen3:1.7b")
prompt = body.get("prompt")
if not prompt:
raise HTTPException(status_code=400, detail="Prompt is required")
url = f"{OLLAMA_API_URL}/api/generate"
async def stream_response():
try:
response = requests.post(
url=url,
json={"model": model, "prompt": prompt, "stream": True},
stream=True
)
for chunk in response.iter_content(chunk_size=None):
yield chunk
except Exception as e:
print(f"Error during streaming: {e}")
return StreamingResponse(stream_response(), media_type="application/x-ndjson")
@app.get("/")
async def root():
return RedirectResponse(url="/chat")
@app.get("/chat")
async def chat_page():
return FileResponse('chat.html')
|