Rahatara commited on
Commit
fcd8eda
1 Parent(s): 13240dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -73
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
- example_model = genai.GenerativeModel(
13
- MODEL_ID,
14
- system_instruction=[
15
- "You are an advocate against gender-based violence.",
16
- "Analyze the content for signs of gender discrimination and provide actionable advice."
17
- ],
18
- )
 
 
 
 
 
19
 
20
- # Set model parameters
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 and instruction settings
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"Analyze this text for any instances of gender-based discrimination and provide tips: {text}"
39
- response = example_model.generate_content(
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
- """Analyzes the uploaded image for gender-based discrimination."""
48
- prompt = "Analyze this image for any instances of gender-based discrimination and provide actionable advice."
49
- resized_image = preprocess_image(image) # Resize the image to the required size
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
- def preprocess_image(image: Image.Image) -> Image.Image:
58
- """Resizes the image maintaining aspect ratio."""
59
- image_height = int(image.height * 512 / image.width)
60
- return image.resize((512, image_height))
61
-
62
- # Example scenarios for gender discrimination analysis
63
- example_scenarios = [
64
- "During a team meeting, whenever a female colleague tried to express her opinion, she was often interrupted or talked over by male colleagues.",
65
- "The feedback given to female employees often focuses more on their demeanor and less on their actual accomplishments.",
66
- "Male employees are more frequently considered for promotions and challenging projects, even when female employees have similar or superior qualifications.",
67
- "During a hiring panel, female candidates were often asked about their personal life, family plans, and how they would balance home and work.",
68
- "There are significant wage discrepancies between male and female employees who hold the same position and possess comparable experience.",
69
- "Some male colleagues often make inappropriate jokes or comments about female employees' appearances and attire."
70
- ]
71
-
72
- example_images = [["ex1.jpg"],["ex2.png"]]
73
-
74
- css_style = """
75
- body, .gradio-container {
76
- background-color: #020308; /* Replace with your preferred color */
77
- }
78
-
79
- #logo {
80
- display: flex;
81
- justify-content: center;
82
- font-size: 3em;
83
- font-weight: bold;
84
- letter-spacing: 3px;
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
- gr.Markdown("<p style='text-align: center; font-size: 16px; color: #f0f0f0;'>Powered by Gemini to advocate against gender-based violence</p>")
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
- examples = gr.Examples(
124
- examples=example_scenarios,
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
- examples = gr.Examples(
135
- examples=example_images,
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()