jfforero commited on
Commit
24a178b
·
verified ·
1 Parent(s): 5df7fc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -145,49 +145,43 @@ def generate_music(transcribed_text, emotion_prediction):
145
  print("Error generating music:", e)
146
  return None
147
 
 
148
  api_key = os.getenv("DeepAI_api_key")
149
 
150
- # Function to generate an image using DeepAI Text to Image API
151
- def generate_image(emotion_prediction, transcribed_text, output_resolution=(1024, 1024)):
152
  try:
153
  if not api_key:
154
- return "API key not found"
 
155
 
156
- url = "https://api.deepai.org/api/image-editor"
157
- headers = {
158
- 'api-key': api_key
159
- }
160
 
161
- # Select a random image file from TerraIncognita0.jpg to TerraIncognita9.jpg
162
- random_index = random.randint(0, 9)
163
- image_file_path = f'TAI_Images/TerraIncognita{random_index}.jpg'
 
 
 
 
 
 
 
 
164
 
165
- # Check if the file exists
166
- if not os.path.exists(image_file_path):
167
- return f"Image file not found: {image_file_path}"
168
-
169
- prompt_text = f"Generate Patagonian Monsters' with a {emotion_prediction} attitude, representing the idea of: [ {transcribed_text} ]. Illustrate this using asemic writings in an old map style."
170
-
171
- with open(image_file_path, 'rb') as image_file:
172
- files = {
173
- 'image': image_file,
174
- }
175
- data = {
176
- 'text': prompt_text
177
- }
178
- response = requests.post(url, headers=headers, files=files, data=data)
179
-
180
- response_data = response.json()
181
- if 'output_url' in response_data:
182
- # Download the image and return it as a PIL Image
183
- image_response = requests.get(response_data['output_url'])
184
- return Image.open(BytesIO(image_response.content))
185
  else:
186
- print("Error in DeepAI response:", response_data)
187
- return None
 
188
  except Exception as e:
189
  print("Error generating image:", e)
190
- return None
 
191
 
192
  # Function to get predictions
193
  def get_predictions(audio_input):
@@ -201,6 +195,7 @@ def get_predictions(audio_input):
201
  # Analyze sentiment of transcribed text
202
  sentiment, polarity = analyze_sentiment(transcribed_text)
203
 
 
204
  image = generate_image(emotion_prediction, transcribed_text)
205
 
206
  # Generate music based on transcription and emotion
@@ -217,7 +212,7 @@ interface = gr.Interface(
217
  gr.Label(label="Transcribed Text"),
218
  gr.Label(label="Sentiment Analysis"),
219
  gr.Image(type='pil', label="Generated Image"),
220
- gr.Audio(label="Generated Music", type="filepath") # Added music output
221
  ],
222
  title="Affective Virtual Environments",
223
  description="Create an AVE using your voice. Get emotion prediction, transcription, sentiment analysis, a generated image, and music."
 
145
  print("Error generating music:", e)
146
  return None
147
 
148
+ # --- DeepAI Image Generation (Text2Img) ---
149
  api_key = os.getenv("DeepAI_api_key")
150
 
151
+ def generate_image(emotion_prediction, transcribed_text):
 
152
  try:
153
  if not api_key:
154
+ # fallback white image if no API key
155
+ return Image.new('RGB', (512, 512), color='white')
156
 
157
+ # Create the prompt for text2img
158
+ prompt = f"Generate Patagonian Monsters with a {emotion_prediction} attitude, representing the idea of: [{transcribed_text}]. Illustrate this using asemic writings in an old map style."
 
 
159
 
160
+ # Make request to DeepAI text2img API
161
+ response = requests.post(
162
+ "https://api.deepai.org/api/text2img",
163
+ data={
164
+ 'text': prompt,
165
+ 'width': 512,
166
+ 'height': 512,
167
+ 'image_generator_version': 'hd'
168
+ },
169
+ headers={'api-key': api_key}
170
+ )
171
 
172
+ data = response.json()
173
+ if 'output_url' in data:
174
+ # Download the generated image
175
+ img_resp = requests.get(data['output_url'])
176
+ return Image.open(BytesIO(img_resp.content))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  else:
178
+ print("Error in DeepAI response:", data)
179
+ # Return a fallback image
180
+ return Image.new('RGB', (512, 512), color='white')
181
  except Exception as e:
182
  print("Error generating image:", e)
183
+ # Return a fallback image
184
+ return Image.new('RGB', (512, 512), color='white')
185
 
186
  # Function to get predictions
187
  def get_predictions(audio_input):
 
195
  # Analyze sentiment of transcribed text
196
  sentiment, polarity = analyze_sentiment(transcribed_text)
197
 
198
+ # Generate image using text2img
199
  image = generate_image(emotion_prediction, transcribed_text)
200
 
201
  # Generate music based on transcription and emotion
 
212
  gr.Label(label="Transcribed Text"),
213
  gr.Label(label="Sentiment Analysis"),
214
  gr.Image(type='pil', label="Generated Image"),
215
+ gr.Audio(label="Generated Music", type="filepath")
216
  ],
217
  title="Affective Virtual Environments",
218
  description="Create an AVE using your voice. Get emotion prediction, transcription, sentiment analysis, a generated image, and music."