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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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")
8
+ 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,
24
+ top_k=32,
25
+ candidate_count=1,
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,
33
+ genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
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:
106
+ gr.HTML("""
107
+ <div id="logo">
108
+ <span class="letter j">J</span>
109
+ <span class="letter u">u</span>
110
+ <span class="letter s">s</span>
111
+ <span class="letter t">t</span>
112
+ <span class="letter e">E</span>
113
+ <span class="letter v">v</span>
114
+ <span class="letter a">a</span>
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()