Spaces:
Running
Running
import os | |
import google.generativeai as genai | |
import gradio as gr | |
from PIL import Image | |
import moviepy.editor as mp | |
from google.generativeai.types import HarmBlockThreshold, HarmCategory | |
# Configure Google API Key and model | |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") | |
genai.configure(api_key=GOOGLE_API_KEY) | |
MODEL_ID = "gemini-1.5-pro-002" | |
model = genai.GenerativeModel(MODEL_ID) | |
example_model = genai.GenerativeModel( | |
MODEL_ID, | |
system_instruction=[ | |
"You are an advocate against gender-based violence.", | |
"Analyze the content for signs of gender discrimination and provide actionable advice." | |
], | |
) | |
# Set model parameters | |
generation_config = genai.GenerationConfig( | |
temperature=0.9, | |
top_p=1.0, | |
top_k=32, | |
candidate_count=1, | |
max_output_tokens=8192, | |
) | |
# Safety and instruction settings | |
safety_settings = { | |
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, | |
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, | |
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, | |
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, | |
} | |
# Function to analyze text | |
def analyze_text(text): | |
prompt = f"Analyze this text for any instances of gender-based discrimination and provide tips: {text}" | |
contents=[prompt] | |
response = example_model.generate_content( | |
contents, | |
generation_config=generation_config, | |
safety_settings=safety_settings,) | |
return response.text if response else "No response generated." | |
# Example scenarios | |
example_scenarios = [ | |
("During a team meeting, whenever a female colleague tried to express her opinion, she was often interrupted or talked over by male colleagues."), | |
("The feedback given to female employees often focuses more on their demeanor and less on their actual accomplishments."), | |
("Male employees are more frequently considered for promotions and challenging projects, even when female employees have similar or superior qualifications."), | |
("During a hiring panel, female candidates were often asked about their personal life, family plans, and how they would balance home and work."), | |
("There are significant wage discrepancies between male and female employees who hold the same position and possess comparable experience."), | |
("Some male colleagues often make inappropriate jokes or comments about female employees' appearances and attire."), | |
] | |
# Gradio interface setup | |
with gr.Blocks() as app: | |
with gr.Tab("Text Analysis"): | |
text_input = gr.Textbox(label="Enter Text", placeholder="Type here...", lines=4) | |
analyze_text_btn = gr.Button("Analyze Text") | |
text_output = gr.Textbox(label="Analysis Output", lines=6) | |
gr.Examples(examples=example_scenarios, inputs=text_input, fn=analyze_text, outputs=text_output) | |
analyze_text_btn.click( | |
fn=analyze_text, | |
inputs=text_input, | |
outputs=text_output | |
) | |
app.launch() | |