Bhargav3000 commited on
Commit
d038e9b
·
verified ·
1 Parent(s): b082851

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -1,6 +1,27 @@
1
  import gradio as gr
2
  import requests
3
  import base64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def generate_comic(short_story):
6
  sentences = short_story.split('. ')
@@ -8,25 +29,12 @@ def generate_comic(short_story):
8
 
9
  for sentence in sentences:
10
  if sentence.strip():
11
- response = requests.post(
12
- "https://api.deepai.org/api/text2img",
13
- data={'text': sentence},
14
- headers={'api-key': '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'}
15
- )
16
- # Print the response for debugging
17
- print("API Response:", response.json())
18
-
19
- image_url = response.json().get('output_url')
20
- if image_url:
21
- print("Generated Image URL:", image_url) # Debugging URL
22
-
23
- # Convert image to base64
24
- try:
25
- image_data = requests.get(image_url).content
26
- base64_image = base64.b64encode(image_data).decode('utf-8')
27
- images.append(f"data:image/png;base64,{base64_image}")
28
- except Exception as e:
29
- print(f"Error fetching image: {e}")
30
 
31
  # Create comic HTML
32
  comic_html = '<div style="display: flex; flex-direction: column;">'
 
1
  import gradio as gr
2
  import requests
3
  import base64
4
+ import openai
5
+
6
+ # Set your OpenAI API key
7
+ openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API key
8
+
9
+ def generate_images_from_text(text):
10
+ # Generate an image using OpenAI's DALL-E model or similar
11
+ response = openai.Image.create(
12
+ prompt=text,
13
+ n=1,
14
+ size="512x512"
15
+ )
16
+
17
+ image_url = response['data'][0]['url']
18
+
19
+ # Fetch the image from the URL
20
+ image_response = requests.get(image_url)
21
+
22
+ # Convert the image to Base64 for embedding in HTML
23
+ base64_image = base64.b64encode(image_response.content).decode('utf-8')
24
+ return f"data:image/png;base64,{base64_image}"
25
 
26
  def generate_comic(short_story):
27
  sentences = short_story.split('. ')
 
29
 
30
  for sentence in sentences:
31
  if sentence.strip():
32
+ # Generate image from sentence
33
+ try:
34
+ base64_image = generate_images_from_text(sentence)
35
+ images.append(base64_image)
36
+ except Exception as e:
37
+ print(f"Error generating image: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Create comic HTML
40
  comic_html = '<div style="display: flex; flex-direction: column;">'