Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
4 |
+
from fastapi.staticfiles import StaticFiles
|
5 |
+
from pydantic import BaseModel
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
import uvicorn
|
8 |
+
|
9 |
+
# Initialize FastAPI app
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Serve static files for assets
|
13 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
14 |
+
|
15 |
+
# Initialize Hugging Face Inference Client
|
16 |
+
client = InferenceClient()
|
17 |
+
|
18 |
+
# Pydantic model for API input
|
19 |
+
class InfographicRequest(BaseModel):
|
20 |
+
description: str
|
21 |
+
|
22 |
+
# Load prompt template from environment variable
|
23 |
+
PROMPT_TEMPLATE = os.getenv("PROMPT_TEMPLATE")
|
24 |
+
|
25 |
+
# Route to serve the HTML template
|
26 |
+
@app.get("/", response_class=HTMLResponse)
|
27 |
+
async def serve_frontend():
|
28 |
+
with open("infographic_gen.html", "r") as file:
|
29 |
+
return HTMLResponse(content=file.read())
|
30 |
+
|
31 |
+
# Route to handle infographic generation
|
32 |
+
@app.post("/generate")
|
33 |
+
async def generate_infographic(request: InfographicRequest):
|
34 |
+
description = request.description
|
35 |
+
prompt = PROMPT_TEMPLATE.format(description=description)
|
36 |
+
|
37 |
+
try:
|
38 |
+
# Query Hugging Face model
|
39 |
+
messages = [{"role": "user", "content": prompt}]
|
40 |
+
stream = client.chat.completions.create(
|
41 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
42 |
+
messages=messages,
|
43 |
+
temperature=0.5,
|
44 |
+
max_tokens=1024,
|
45 |
+
top_p=0.7,
|
46 |
+
stream=True,
|
47 |
+
)
|
48 |
+
|
49 |
+
# Collect the HTML content from the stream
|
50 |
+
generated_html = ""
|
51 |
+
for chunk in stream:
|
52 |
+
generated_html += chunk.choices[0].delta.content
|
53 |
+
|
54 |
+
# Return the generated HTML content
|
55 |
+
return JSONResponse(content={"html": generated_html})
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|