geethareddy commited on
Commit
bdbc83b
·
verified ·
1 Parent(s): 03e6d52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -100
app.py CHANGED
@@ -1,136 +1,96 @@
1
  import os
2
- import requests
3
  import gradio as gr
 
 
4
  import nltk
5
  from nltk.tokenize import sent_tokenize
6
- from PIL import Image
7
- from io import BytesIO
8
- import base64
 
9
 
10
- # Ensure NLTK's Punkt tokenizer models are downloaded
11
  nltk.download('punkt')
12
 
13
- # Retrieve Hugging Face API key from environment variables
14
- HUGGINGFACE_API_KEY = os.getenv('onteddu')
15
 
16
- if not HUGGINGFACE_API_KEY:
17
- raise ValueError("Please set the HUGGINGFACE_API_KEY environment variable.")
18
 
19
- # Define the Hugging Face Inference API URL for a text-to-image model
20
  # You can choose a different model if preferred
21
- MODEL_NAME = "stabilityai/stable-diffusion-2-1" # Ensure this model is available for your API key
22
- API_URL = f"https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
23
-
24
- headers = {
25
- "Authorization": f"Bearer {HUGGINGFACE_API_KEY}"
26
- }
27
-
28
- def query(prompt):
29
- """
30
- Sends a POST request to the Hugging Face Inference API with the given prompt.
31
- Returns the image bytes if successful.
32
- """
33
- payload = {
34
- "inputs": prompt,
35
- # You can add additional parameters here if needed, such as "parameters": {"num_inference_steps": 50}
36
- }
37
-
38
- response = requests.post(API_URL, headers=headers, json=payload)
39
-
40
- if response.status_code == 200:
41
- # The response is a list of base64-encoded images
42
- response_json = response.json()
43
- if isinstance(response_json, list) and len(response_json) > 0:
44
- image_data = response_json[0] # Get the first image
45
- # The image data is a data URI: "data:image/png;base64,..."
46
- header, encoded = image_data.split(",", 1)
47
- image_bytes = base64.b64decode(encoded)
48
- return image_bytes
49
- else:
50
- raise ValueError("Unexpected response format from Hugging Face API.")
51
- else:
52
- # Attempt to parse error message from response
53
- try:
54
- error_info = response.json()
55
- error_message = error_info.get('error', 'No error message provided.')
56
- except ValueError:
57
- error_message = response.text
58
- raise Exception(f"Request failed with status code {response.status_code}: {error_message}")
59
 
60
- def generate_comic(story):
61
  """
62
- Takes a short story, splits it into sentences, generates an image for each sentence,
63
- and returns a list of PIL Image objects.
64
  """
65
  # Split the story into sentences
66
  sentences = sent_tokenize(story)
67
-
 
 
 
68
  images = []
 
69
  for idx, sentence in enumerate(sentences):
 
 
 
70
  try:
71
- print(f"Generating image for sentence {idx+1}/{len(sentences)}: {sentence}")
72
- # Query the Hugging Face API
73
- image_bytes = query(sentence)
74
- # Open the image
75
- image = Image.open(BytesIO(image_bytes)).convert("RGB")
76
- images.append(image)
 
 
 
 
 
 
 
 
 
 
77
  except Exception as e:
78
- print(f"Error generating image for sentence '{sentence}': {e}")
79
- # Append a placeholder image with error text
80
- placeholder = Image.new('RGB', (512, 512), color=(255, 0, 0))
81
- draw = Image.Draw.Draw(placeholder)
82
- draw.text((10, 10), "Error generating image", fill=(255, 255, 255))
83
- images.append(placeholder)
84
-
85
  return images
86
 
87
  def create_comic_panel(images):
88
  """
89
- Combines a list of PIL Images into a single image arranged horizontally
90
- to mimic comic panels.
91
  """
92
- # Filter out any None images
93
- images = [img for img in images if img is not None]
94
-
95
- if not images:
96
- return None
97
-
98
- # Define the size for each panel
99
- panel_width, panel_height = 512, 512 # You can adjust as needed
100
-
101
- # Resize images to uniform size
102
- resized_images = [img.resize((panel_width, panel_height)) for img in images]
103
-
104
- # Create a new blank image with enough width to hold all panels
105
- total_width = panel_width * len(resized_images)
106
- max_height = panel_height
107
- comic_image = Image.new('RGB', (total_width, max_height), color=(255, 255, 255))
108
-
109
- # Paste each panel image side by side
110
- for i, img in enumerate(resized_images):
111
- comic_image.paste(img, (i * panel_width, 0))
112
-
113
- return comic_image
114
 
115
  def process_story(story):
116
  """
117
- Processes the input story and returns a comic panel image.
118
  """
119
- images = generate_comic(story)
120
- comic = create_comic_panel(images)
121
- return comic
122
 
123
- # Define the Gradio interface
124
  iface = gr.Interface(
125
  fn=process_story,
126
  inputs=gr.Textbox(lines=10, placeholder="Enter your short story here...", label="Short Story"),
127
- outputs=gr.Image(type="pil", label="Generated Comic"),
128
- title="Story to Comic Generator",
129
- description="Enter a short story, and this app will generate a comic-style image with one panel per sentence.",
130
  examples=[
131
- ["Once upon a time, there was a brave knight. He fought dragons and saved the kingdom."]
132
- ],
133
- allow_flagging="never"
134
  )
135
 
136
  if __name__ == "__main__":
 
1
  import os
 
2
  import gradio as gr
3
+ import requests
4
+ from huggingface_hub import InferenceApi
5
  import nltk
6
  from nltk.tokenize import sent_tokenize
7
+ from dotenv import load_dotenv
8
+
9
+ # Load environment variables from .env file if present
10
+ load_dotenv()
11
 
12
+ # Ensure NLTK sentence tokenizer data is downloaded
13
  nltk.download('punkt')
14
 
15
+ # Retrieve Hugging Face API key from environment variable
16
+ HF_API_KEY = os.getenv('HUGGINGFACE_API_KEY')
17
 
18
+ if not HF_API_KEY:
19
+ raise ValueError("Please set the 'HUGGINGFACE_API_KEY' environment variable.")
20
 
21
+ # Initialize Hugging Face Inference API with a suitable image generation model
22
  # You can choose a different model if preferred
23
+ MODEL_NAME = "stabilityai/stable-diffusion-2"
24
+ inference = InferenceApi(repo_id=MODEL_NAME, token=HF_API_KEY)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ def generate_comic_images(story):
27
  """
28
+ Takes a short story as input, splits it into sentences, generates an image for each sentence,
29
+ and returns the list of image URLs.
30
  """
31
  # Split the story into sentences
32
  sentences = sent_tokenize(story)
33
+
34
+ if len(sentences) == 0:
35
+ return ["No sentences found in the input story."]
36
+
37
  images = []
38
+
39
  for idx, sentence in enumerate(sentences):
40
+ # Optionally, you can modify the prompt to better suit comic-style images
41
+ prompt = f"Comic book style illustration of: {sentence}"
42
+
43
  try:
44
+ # Generate image using Hugging Face Inference API
45
+ output = inference(prompt)
46
+
47
+ # The output can be a URL or binary data depending on the model
48
+ # Here we assume it's a URL to the generated image
49
+ if isinstance(output, str):
50
+ image_url = output
51
+ elif isinstance(output, dict) and 'image' in output:
52
+ image_url = output['image']
53
+ else:
54
+ image_url = None
55
+
56
+ if image_url:
57
+ images.append(image_url)
58
+ else:
59
+ images.append("Image generation failed for this sentence.")
60
  except Exception as e:
61
+ images.append(f"Error: {str(e)}")
62
+
 
 
 
 
 
63
  return images
64
 
65
  def create_comic_panel(images):
66
  """
67
+ Arranges images in a comic panel layout.
 
68
  """
69
+ # Create a list of Gradio Image components with captions
70
+ image_components = []
71
+ for idx, img in enumerate(images):
72
+ image_components.append(gr.Image(value=img, label=f"Sentence {idx+1}"))
73
+
74
+ return image_components
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  def process_story(story):
77
  """
78
+ Main processing function to generate images and arrange them in a comic panel.
79
  """
80
+ images = generate_comic_images(story)
81
+ image_components = create_comic_panel(images)
82
+ return image_components
83
 
84
+ # Define Gradio interface
85
  iface = gr.Interface(
86
  fn=process_story,
87
  inputs=gr.Textbox(lines=10, placeholder="Enter your short story here...", label="Short Story"),
88
+ outputs=gr.Column(),
89
+ title="Comic Generator",
90
+ description="Enter a short story, and generate a comic-style image for each sentence using Hugging Face's API.",
91
  examples=[
92
+ ["Once upon a time, there was a brave knight. He fought dragons and rescued princesses."]
93
+ ]
 
94
  )
95
 
96
  if __name__ == "__main__":