dschandra commited on
Commit
4347ff0
·
verified ·
1 Parent(s): 286723a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -17
app.py CHANGED
@@ -2,32 +2,31 @@ import gradio as gr
2
  import requests
3
  from PIL import Image
4
  import io
5
- import base64
6
 
7
  # Define the API URL for the Craiyon model (lightweight text-to-image generator)
8
  API_URL = "https://api-inference.huggingface.co/models/dalle-mini"
9
 
10
  # Function to call the model and generate images
11
  def generate_comic(prompt):
12
- headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"}
 
 
13
 
14
  # Sending request to the model with user prompt
15
  response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
16
 
17
- # Print the response for debugging
18
- print(f"API Response: {response.status_code}, {response.text}")
19
-
20
  # Check if the response is successful
21
  if response.status_code != 200:
22
  return f"Error: {response.status_code}, {response.text}"
23
 
24
- # Extracting generated images (ensure this key is correct, it might differ depending on the model)
25
  images = response.json().get("generated_images", [])
26
 
27
  if not images:
28
  return "No images were generated, please try again with a different prompt."
29
 
30
- # Process the images (assuming they are base64 encoded)
31
  pil_images = []
32
  for img in images:
33
  image_data = base64.b64decode(img)
@@ -38,21 +37,12 @@ def generate_comic(prompt):
38
 
39
  # Gradio interface setup
40
  def gradio_interface():
41
- # Set up input and output elements
42
  with gr.Blocks() as demo:
43
  gr.Markdown("## GenArt Narrative - Turn Your Story into Comic Panels!")
44
-
45
- with gr.Row():
46
- prompt = gr.Textbox(label="Enter your short story description", placeholder="Once upon a time...")
47
-
48
- # Directly specifying the number of columns without .style()
49
  output_gallery = gr.Gallery(label="Generated Comic Panels", columns=3, height=300)
50
-
51
  submit_button = gr.Button("Generate Comic")
52
-
53
- # Set up button functionality
54
  submit_button.click(fn=generate_comic, inputs=prompt, outputs=output_gallery)
55
-
56
  return demo
57
 
58
  # Run the Gradio app
 
2
  import requests
3
  from PIL import Image
4
  import io
5
+ import os
6
 
7
  # Define the API URL for the Craiyon model (lightweight text-to-image generator)
8
  API_URL = "https://api-inference.huggingface.co/models/dalle-mini"
9
 
10
  # Function to call the model and generate images
11
  def generate_comic(prompt):
12
+ api_token = os.environ.get("API_TOKEN") # Securely access the token
13
+
14
+ headers = {"Authorization": f"Bearer {api_token}"}
15
 
16
  # Sending request to the model with user prompt
17
  response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
18
 
 
 
 
19
  # Check if the response is successful
20
  if response.status_code != 200:
21
  return f"Error: {response.status_code}, {response.text}"
22
 
23
+ # Extracting the response data
24
  images = response.json().get("generated_images", [])
25
 
26
  if not images:
27
  return "No images were generated, please try again with a different prompt."
28
 
29
+ # Process the images (assuming they are base64 encoded or URL)
30
  pil_images = []
31
  for img in images:
32
  image_data = base64.b64decode(img)
 
37
 
38
  # Gradio interface setup
39
  def gradio_interface():
 
40
  with gr.Blocks() as demo:
41
  gr.Markdown("## GenArt Narrative - Turn Your Story into Comic Panels!")
42
+ prompt = gr.Textbox(label="Enter your short story description", placeholder="Once upon a time...")
 
 
 
 
43
  output_gallery = gr.Gallery(label="Generated Comic Panels", columns=3, height=300)
 
44
  submit_button = gr.Button("Generate Comic")
 
 
45
  submit_button.click(fn=generate_comic, inputs=prompt, outputs=output_gallery)
 
46
  return demo
47
 
48
  # Run the Gradio app