Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,9 @@ import os
|
|
2 |
from PIL import Image
|
3 |
import google.generativeai as genai
|
4 |
import gradio as gr
|
|
|
|
|
|
|
5 |
|
6 |
# Configure Google API Key and model
|
7 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
@@ -9,15 +12,20 @@ genai.configure(api_key=GOOGLE_API_KEY)
|
|
9 |
MODEL_ID = "gemini-1.5-pro-latest"
|
10 |
model = genai.GenerativeModel(MODEL_ID)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
generation_config = genai.GenerationConfig(
|
22 |
temperature=0.9,
|
23 |
top_p=1.0,
|
@@ -26,7 +34,7 @@ generation_config = genai.GenerationConfig(
|
|
26 |
max_output_tokens=8192,
|
27 |
)
|
28 |
|
29 |
-
# Safety
|
30 |
safety_settings = {
|
31 |
genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
32 |
genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
@@ -34,72 +42,70 @@ safety_settings = {
|
|
34 |
genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
35 |
}
|
36 |
|
|
|
37 |
def analyze_text(text):
|
38 |
-
prompt = f"
|
39 |
-
response =
|
40 |
[prompt],
|
41 |
generation_config=generation_config,
|
42 |
safety_settings=safety_settings,
|
43 |
)
|
44 |
return response.text if response else "No response generated."
|
45 |
|
|
|
46 |
def analyze_image(image: Image.Image) -> str:
|
47 |
-
"
|
48 |
-
|
49 |
-
|
50 |
-
response = example_model.generate_content(
|
51 |
[prompt, resized_image],
|
52 |
generation_config=generation_config,
|
53 |
safety_settings=safety_settings,
|
54 |
)
|
55 |
return response.text if response else "No response generated."
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
return
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
}
|
86 |
-
.letter {
|
87 |
-
opacity: 0;
|
88 |
-
animation: fadeIn 0.1s forwards;
|
89 |
-
}
|
90 |
-
.letter.j { animation-delay: 0s; color: #4285F4; } /* Blue */
|
91 |
-
.letter.u { animation-delay: 0.1s; color: #3A9CF1; }
|
92 |
-
.letter.s { animation-delay: 0.2s; color: #32B3EE; }
|
93 |
-
.letter.t { animation-delay: 0.3s; color: #2BC9EA; }
|
94 |
-
.letter.e { animation-delay: 0.4s; color: #23E0E7; }
|
95 |
-
.letter.v { animation-delay: 0.5s; color: #1BF7E4; }
|
96 |
-
.letter.a { animation-delay: 0.6s; color: #14F0B5; } /* Greenish */
|
97 |
-
@keyframes fadeIn {
|
98 |
-
0% { opacity: 0; transform: translateY(-20px); }
|
99 |
-
100% { opacity: 1; transform: translateY(0); }
|
100 |
-
}
|
101 |
-
"""
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
# Gradio interface setup
|
105 |
with gr.Blocks(css=css_style) as app:
|
@@ -115,27 +121,23 @@ with gr.Blocks(css=css_style) as app:
|
|
115 |
</div>
|
116 |
""")
|
117 |
gr.Markdown("<h1 style='text-align: center; color:#f0f0f0;'>Promotes Gender Equality in Every Conversation</h1>")
|
118 |
-
|
119 |
with gr.Tab("Text Analysis"):
|
120 |
text_input = gr.Textbox(label="Enter Text or Select an Example", placeholder="Type here or select an example...", lines=4)
|
121 |
text_output = gr.Textbox(label="Analysis Output", lines=6)
|
122 |
analyze_text_btn = gr.Button("Analyze Text")
|
123 |
-
|
124 |
-
|
125 |
-
inputs=text_input,
|
126 |
-
outputs=text_output
|
127 |
-
)
|
128 |
analyze_text_btn.click(analyze_text, inputs=text_input, outputs=text_output)
|
|
|
129 |
|
130 |
with gr.Tab("Image Analysis"):
|
131 |
-
image_input = gr.Image(label="Upload Image(e.g., screenshot, photos, etc.)", type="pil")
|
132 |
image_output = gr.Textbox(label="Analysis Output", lines=6)
|
133 |
analyze_image_btn = gr.Button("Analyze Image")
|
134 |
-
|
135 |
-
|
136 |
-
inputs=image_input,
|
137 |
-
outputs=image_output
|
138 |
-
)
|
139 |
analyze_image_btn.click(analyze_image, inputs=image_input, outputs=image_output)
|
|
|
140 |
|
141 |
-
app.launch()
|
|
|
2 |
from PIL import Image
|
3 |
import google.generativeai as genai
|
4 |
import gradio as gr
|
5 |
+
from gtts import gTTS
|
6 |
+
from pydub import AudioSegment
|
7 |
+
import tempfile
|
8 |
|
9 |
# Configure Google API Key and model
|
10 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
12 |
MODEL_ID = "gemini-1.5-pro-latest"
|
13 |
model = genai.GenerativeModel(MODEL_ID)
|
14 |
|
15 |
+
# System prompts
|
16 |
+
analysis_system_prompt = "You are an expert in gender studies. Analyze the following content for any signs of gender-based discrimination and suggest actionable advice."
|
17 |
+
podcast_prompt = """You are Eva, a solo podcast host focusing on gender equality topics.
|
18 |
+
- Discuss real-life scenarios involving gender-based discrimination, provide insights, and offer solutions in a conversational, storytelling style.
|
19 |
+
- Based on the analyzed text, create an engaging solo podcast as if reading stories from different victims who send you their story.
|
20 |
+
- Introduce yourself as Eva.
|
21 |
+
- Keep the conversation within 30000 characters, with a lot of emotion.
|
22 |
+
- Use short sentences suitable for speech synthesis.
|
23 |
+
- Maintain an empathetic tone.
|
24 |
+
- Include filler words like 'äh' for a natural flow.
|
25 |
+
- Avoid background music or extra words.
|
26 |
+
"""
|
27 |
|
28 |
+
# Model generation configuration
|
29 |
generation_config = genai.GenerationConfig(
|
30 |
temperature=0.9,
|
31 |
top_p=1.0,
|
|
|
34 |
max_output_tokens=8192,
|
35 |
)
|
36 |
|
37 |
+
# Safety settings
|
38 |
safety_settings = {
|
39 |
genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
40 |
genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
|
|
42 |
genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
43 |
}
|
44 |
|
45 |
+
# Analyze text
|
46 |
def analyze_text(text):
|
47 |
+
prompt = f"{analysis_system_prompt}\nContent:\n{text}"
|
48 |
+
response = model.generate_content(
|
49 |
[prompt],
|
50 |
generation_config=generation_config,
|
51 |
safety_settings=safety_settings,
|
52 |
)
|
53 |
return response.text if response else "No response generated."
|
54 |
|
55 |
+
# Analyze image
|
56 |
def analyze_image(image: Image.Image) -> str:
|
57 |
+
prompt = f"{analysis_system_prompt}\nAnalyze this image for any instances of gender-based discrimination."
|
58 |
+
resized_image = preprocess_image(image)
|
59 |
+
response = model.generate_content(
|
|
|
60 |
[prompt, resized_image],
|
61 |
generation_config=generation_config,
|
62 |
safety_settings=safety_settings,
|
63 |
)
|
64 |
return response.text if response else "No response generated."
|
65 |
|
66 |
+
# Preprocess image by resizing
|
67 |
+
def preprocess_image(image: Image.Image) -> str:
|
68 |
+
image = image.resize((512, int(image.height * 512 / image.width)))
|
69 |
+
return "a detailed analysis of the visual content, focusing on gender-based discrimination aspects"
|
70 |
+
|
71 |
+
# Generate podcast script
|
72 |
+
def generate_podcast_script(content):
|
73 |
+
prompt = f"{podcast_prompt}\nAnalyzed content:\n{content}"
|
74 |
+
response = model.generate_content([prompt], generation_config=generation_config)
|
75 |
+
script = response.text if response else "Eva has no commentary at this time."
|
76 |
+
return script
|
77 |
+
|
78 |
+
# Convert script to audio using gTTS
|
79 |
+
def text_to_speech(script):
|
80 |
+
lines = [line.strip() for line in script.split(".") if line.strip()] # Split by sentences for manageable TTS segments
|
81 |
+
audio_files = []
|
82 |
+
|
83 |
+
for line in lines:
|
84 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
85 |
+
try:
|
86 |
+
tts = gTTS(text=line, lang='en', tld='com') # Using 'com' for American accent
|
87 |
+
tts.save(temp_file.name)
|
88 |
+
sound = AudioSegment.from_mp3(temp_file.name)
|
89 |
+
sound += AudioSegment.silent(duration=500) # Add a 0.5-second pause after each sentence
|
90 |
+
sound.export(temp_file.name, format="mp3")
|
91 |
+
audio_files.append(temp_file.name)
|
92 |
+
except Exception as e:
|
93 |
+
print(f"Error generating audio for line '{line}': {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
combined_audio = AudioSegment.empty()
|
96 |
+
for file in audio_files:
|
97 |
+
sound = AudioSegment.from_mp3(file)
|
98 |
+
combined_audio += sound
|
99 |
+
os.remove(file) # Clean up temporary files
|
100 |
+
|
101 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
102 |
+
combined_audio.export(output_file.name, format="mp3")
|
103 |
+
return output_file.name
|
104 |
+
|
105 |
+
# Generate and play podcast
|
106 |
+
def generate_and_play_podcast(content, content_type='text'):
|
107 |
+
script = generate_podcast_script(content)
|
108 |
+
return text_to_speech(script)
|
109 |
|
110 |
# Gradio interface setup
|
111 |
with gr.Blocks(css=css_style) as app:
|
|
|
121 |
</div>
|
122 |
""")
|
123 |
gr.Markdown("<h1 style='text-align: center; color:#f0f0f0;'>Promotes Gender Equality in Every Conversation</h1>")
|
124 |
+
|
125 |
with gr.Tab("Text Analysis"):
|
126 |
text_input = gr.Textbox(label="Enter Text or Select an Example", placeholder="Type here or select an example...", lines=4)
|
127 |
text_output = gr.Textbox(label="Analysis Output", lines=6)
|
128 |
analyze_text_btn = gr.Button("Analyze Text")
|
129 |
+
listen_podcast_btn = gr.Button("Listen to Eva")
|
130 |
+
|
|
|
|
|
|
|
131 |
analyze_text_btn.click(analyze_text, inputs=text_input, outputs=text_output)
|
132 |
+
listen_podcast_btn.click(generate_and_play_podcast, inputs=text_output, outputs=gr.Audio())
|
133 |
|
134 |
with gr.Tab("Image Analysis"):
|
135 |
+
image_input = gr.Image(label="Upload Image (e.g., screenshot, photos, etc.)", type="pil")
|
136 |
image_output = gr.Textbox(label="Analysis Output", lines=6)
|
137 |
analyze_image_btn = gr.Button("Analyze Image")
|
138 |
+
listen_podcast_image_btn = gr.Button("Listen to Eva")
|
139 |
+
|
|
|
|
|
|
|
140 |
analyze_image_btn.click(analyze_image, inputs=image_input, outputs=image_output)
|
141 |
+
listen_podcast_image_btn.click(generate_and_play_podcast, inputs=image_output, outputs=gr.Audio())
|
142 |
|
143 |
+
app.launch()
|