Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
import gradio as gr
|
4 |
+
from google.generativeai.types import HarmBlockThreshold, HarmCategory
|
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-002"
|
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 |
+
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
32 |
+
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
33 |
+
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
34 |
+
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
|
35 |
+
}
|
36 |
+
|
37 |
+
# Function to analyze text with error handling
|
38 |
+
def analyze_text(text):
|
39 |
+
prompt = f"Analyze this text for any instances of gender-based discrimination and provide tips: {text}"
|
40 |
+
contents = [prompt]
|
41 |
+
response = example_model.generate_content(
|
42 |
+
contents,
|
43 |
+
generation_config=generation_config,
|
44 |
+
safety_settings=safety_settings,
|
45 |
+
)
|
46 |
+
return response.text if response else "No response generated."
|
47 |
+
|
48 |
+
# Example scenarios for gender discrimination analysis
|
49 |
+
example_scenarios = [
|
50 |
+
"During a team meeting, whenever a female colleague tried to express her opinion, she was often interrupted or talked over by male colleagues.",
|
51 |
+
"The feedback given to female employees often focuses more on their demeanor and less on their actual accomplishments.",
|
52 |
+
"Male employees are more frequently considered for promotions and challenging projects, even when female employees have similar or superior qualifications.",
|
53 |
+
"During a hiring panel, female candidates were often asked about their personal life, family plans, and how they would balance home and work.",
|
54 |
+
"There are significant wage discrepancies between male and female employees who hold the same position and possess comparable experience.",
|
55 |
+
"Some male colleagues often make inappropriate jokes or comments about female employees' appearances and attire."
|
56 |
+
]
|
57 |
+
|
58 |
+
# Gradio interface setup
|
59 |
+
with gr.Blocks() as app:
|
60 |
+
with gr.Tab("Text Analysis"):
|
61 |
+
text_input = gr.Textbox(label="Enter Text or Select an Example", placeholder="Type here or select an example...", lines=4)
|
62 |
+
analyze_text_btn = gr.Button("Analyze Text")
|
63 |
+
text_output = gr.Textbox(label="Analysis Output", lines=6)
|
64 |
+
|
65 |
+
examples = gr.Examples(
|
66 |
+
examples=example_scenarios,
|
67 |
+
inputs=text_input,
|
68 |
+
outputs=text_output,
|
69 |
+
fn=analyze_text # Automatically analyze when an example is selected
|
70 |
+
)
|
71 |
+
|
72 |
+
analyze_text_btn.click(
|
73 |
+
fn=analyze_text,
|
74 |
+
inputs=text_input,
|
75 |
+
outputs=text_output
|
76 |
+
)
|
77 |
+
|
78 |
+
app.launch()
|