bennet009871 commited on
Commit
cb1ed60
·
verified ·
1 Parent(s): f047371

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Request
3
+ from fastapi.responses import JSONResponse
4
+ from pydantic import BaseModel
5
+ from huggingface_hub import InferenceClient
6
+ import gradio as gr
7
+ import re
8
+
9
+ # Initialize FastAPI app
10
+ app = FastAPI()
11
+
12
+ # Initialize Hugging Face Inference Client
13
+ clientHFInference = InferenceClient()
14
+
15
+ # Pydantic model for API input
16
+ class InfographicRequest(BaseModel):
17
+ description: str
18
+
19
+ # Load prompt template from environment variable
20
+ SYSTEM_INSTRUCT = os.getenv("SYSTEM_INSTRUCTOR", "Generate a high-quality infographic based on the description provided.")
21
+ PROMPT_TEMPLATE = os.getenv("PROMPT_TEMPLATE", "Create an infographic with the following details: {description}")
22
+
23
+ async def extract_code_blocks(markdown_text):
24
+ """
25
+ Extracts code blocks from the given Markdown text.
26
+ """
27
+ code_block_pattern = re.compile(r'```.*?\n(.*?)```', re.DOTALL)
28
+ code_blocks = code_block_pattern.findall(markdown_text)
29
+ return code_blocks
30
+
31
+ @app.post("/generate")
32
+ async def generate_infographic(request: InfographicRequest):
33
+ description = request.description
34
+ prompt = PROMPT_TEMPLATE.format(description=description)
35
+
36
+ response = clientHFInference.text_to_image(
37
+ model="stabilityai/stable-diffusion-xl", # Using an advanced image-based AI model
38
+ inputs=prompt
39
+ )
40
+
41
+ generated_image_url = response.get("image_url", None)
42
+
43
+ if generated_image_url:
44
+ return JSONResponse(content={"image_url": generated_image_url})
45
+ else:
46
+ return JSONResponse(content={"error": "No infographic generated"}, status_code=500)
47
+
48
+ # Gradio UI for Hugging Face Spaces
49
+ def generate_infographic_ui(description):
50
+ response = generate_infographic(InfographicRequest(description=description))
51
+ return response["image_url"] if "image_url" in response else "Error generating infographic"
52
+
53
+ demo = gr.Interface(
54
+ fn=generate_infographic_ui,
55
+ inputs="text",
56
+ outputs="image",
57
+ title="AI Infographic Generator",
58
+ description="Enter a description, and the AI will generate a high-quality infographic."
59
+ )
60
+
61
+ demo.launch(share=True)