Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
|
6 |
+
# Define the API URL for the Craiyon model (lightweight text-to-image generator)
|
7 |
+
API_URL = "https://api-inference.huggingface.co/models/dalle-mini"
|
8 |
+
|
9 |
+
# Function to call the model and generate images
|
10 |
+
def generate_comic(prompt):
|
11 |
+
headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"}
|
12 |
+
|
13 |
+
# Sending request to the model with user prompt
|
14 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
15 |
+
|
16 |
+
# Checking for successful response
|
17 |
+
if response.status_code != 200:
|
18 |
+
return f"Error: {response.status_code}, {response.text}"
|
19 |
+
|
20 |
+
# Extracting generated images
|
21 |
+
images = response.json()["generated_images"]
|
22 |
+
|
23 |
+
# Convert image bytes into PIL Image format
|
24 |
+
pil_images = [Image.open(io.BytesIO(img)) for img in images]
|
25 |
+
|
26 |
+
return pil_images # Return the list of PIL images
|
27 |
+
|
28 |
+
# Gradio interface setup
|
29 |
+
def gradio_interface():
|
30 |
+
# Set up input and output elements
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("## GenArt Narrative - Turn Your Story into Comic Panels!")
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
prompt = gr.Textbox(label="Enter your short story description", placeholder="Once upon a time...")
|
36 |
+
|
37 |
+
output_gallery = gr.Gallery(label="Generated Comic Panels").style(grid=[3], height="300px")
|
38 |
+
|
39 |
+
submit_button = gr.Button("Generate Comic")
|
40 |
+
|
41 |
+
# Set up button functionality
|
42 |
+
submit_button.click(fn=generate_comic, inputs=prompt, outputs=output_gallery)
|
43 |
+
|
44 |
+
return demo
|
45 |
+
|
46 |
+
# Run the Gradio app
|
47 |
+
if __name__ == "__main__":
|
48 |
+
app = gradio_interface()
|
49 |
+
app.launch()
|
50 |
+
|