Update app.py
Browse files
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 |
-
|
| 151 |
-
def generate_image(emotion_prediction, transcribed_text, output_resolution=(1024, 1024)):
|
| 152 |
try:
|
| 153 |
if not api_key:
|
| 154 |
-
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
'api-key': api_key
|
| 159 |
-
}
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
if
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 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:",
|
| 187 |
-
|
|
|
|
| 188 |
except Exception as e:
|
| 189 |
print("Error generating image:", e)
|
| 190 |
-
|
|
|
|
| 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")
|
| 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."
|