Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from fastapi.staticfiles import StaticFiles
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
import re
|
|
|
|
| 8 |
|
| 9 |
# Initialize FastAPI app
|
| 10 |
app = FastAPI()
|
|
@@ -13,13 +14,16 @@ app = FastAPI()
|
|
| 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 |
|
|
@@ -41,6 +45,24 @@ async def extract_code_blocks(markdown_text):
|
|
| 41 |
|
| 42 |
return code_blocks
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Route to serve the HTML template
|
| 45 |
@app.get("/", response_class=HTMLResponse)
|
| 46 |
async def serve_frontend():
|
|
@@ -53,23 +75,24 @@ async def generate_infographic(request: InfographicRequest):
|
|
| 53 |
prompt = PROMPT_TEMPLATE.format(description=description)
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
messages = [{"role": "user", "content": prompt}]
|
| 57 |
-
stream = client.chat.completions.create(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
| 65 |
|
| 66 |
|
| 67 |
-
generated_text = ""
|
| 68 |
-
for chunk in stream:
|
| 69 |
-
|
| 70 |
|
| 71 |
-
print(generated_text)
|
| 72 |
-
code_blocks= await extract_code_blocks(generated_text)
|
|
|
|
| 73 |
if code_blocks:
|
| 74 |
return JSONResponse(content={"html": code_blocks[0]})
|
| 75 |
else:
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
import re
|
| 8 |
+
from groq import Groq
|
| 9 |
|
| 10 |
# Initialize FastAPI app
|
| 11 |
app = FastAPI()
|
|
|
|
| 14 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
|
| 16 |
# Initialize Hugging Face Inference Client
|
| 17 |
+
#client = InferenceClient()
|
| 18 |
+
|
| 19 |
+
client = Groq()
|
| 20 |
|
| 21 |
# Pydantic model for API input
|
| 22 |
class InfographicRequest(BaseModel):
|
| 23 |
description: str
|
| 24 |
|
| 25 |
# Load prompt template from environment variable
|
| 26 |
+
SYSTEM_INSTRUCT = = os.getenv("SYSTEM_INSTRUCTOR")
|
| 27 |
PROMPT_TEMPLATE = os.getenv("PROMPT_TEMPLATE")
|
| 28 |
|
| 29 |
|
|
|
|
| 45 |
|
| 46 |
return code_blocks
|
| 47 |
|
| 48 |
+
async def generate_infographic(infoRequest):
|
| 49 |
+
prompt = PROMPT_TEMPLATE.format(description=description)
|
| 50 |
+
generated_completion = client.chat.completions.create(
|
| 51 |
+
model="llama-3.1-70b-versatile",
|
| 52 |
+
messages=[
|
| 53 |
+
{"role": "system", "content": SYSTEM_INSTRUCT},
|
| 54 |
+
{"role": "user", "content": prompt}
|
| 55 |
+
],
|
| 56 |
+
temperature=0.5,
|
| 57 |
+
max_tokens=5000,
|
| 58 |
+
top_p=1,
|
| 59 |
+
stream=False,
|
| 60 |
+
stop=None
|
| 61 |
+
)
|
| 62 |
+
generated_text = generated_completion.choices[0].message.content
|
| 63 |
+
code_blocks= await extract_code_blocks(generated_text)
|
| 64 |
+
return code_blocks
|
| 65 |
+
|
| 66 |
# Route to serve the HTML template
|
| 67 |
@app.get("/", response_class=HTMLResponse)
|
| 68 |
async def serve_frontend():
|
|
|
|
| 75 |
prompt = PROMPT_TEMPLATE.format(description=description)
|
| 76 |
|
| 77 |
try:
|
| 78 |
+
# messages = [{"role": "user", "content": prompt}]
|
| 79 |
+
# stream = client.chat.completions.create(
|
| 80 |
+
# model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 81 |
+
# messages=messages,
|
| 82 |
+
# temperature=0.4,
|
| 83 |
+
# max_tokens=6000,
|
| 84 |
+
# top_p=0.7,
|
| 85 |
+
# stream=True,
|
| 86 |
+
# )
|
| 87 |
|
| 88 |
|
| 89 |
+
# generated_text = ""
|
| 90 |
+
# for chunk in stream:
|
| 91 |
+
# generated_text += chunk.choices[0].delta.content
|
| 92 |
|
| 93 |
+
# print(generated_text)
|
| 94 |
+
#code_blocks= await extract_code_blocks(generated_text)
|
| 95 |
+
code_blocks=generate_infographic(description)
|
| 96 |
if code_blocks:
|
| 97 |
return JSONResponse(content={"html": code_blocks[0]})
|
| 98 |
else:
|