Spaces:
Sleeping
Sleeping
File size: 3,267 Bytes
29ffd49 dd51152 29ffd49 b25d23d 953b1da b25d23d b1a4795 0563efd b1a4795 aab427a b1a4795 32f04bc b1a4795 a756cb9 b1a4795 b25d23d dd51152 3e18d30 32f04bc dd51152 a756cb9 32f04bc 73d5fa3 32f04bc dd51152 a756cb9 32f04bc dd51152 a756cb9 32f04bc dd51152 32f04bc dd51152 b1a4795 32f04bc dd51152 32f04bc b25d23d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 87 88 89 90 91 |
import os
import google.generativeai as genai
import gradio as gr
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 with error handling
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."
# Custom CSS for branding and animation
css = """
body { font-family: 'Arial', sans-serif; background-color: #f0f0f0; }
h1 { color: #333; animation: fadeIn 2s; }
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
#logo { font-size: 48px; color: #5f4b8b; animation: slideIn 2s; }
@keyframes slideIn {
0% { margin-left: -200px; }
100% { margin-left: 0px; }
}
.button { background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; cursor: pointer; width: 100%; }
.button:hover { opacity: 0.8; }
.textbox { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; box-sizing: border-box; }
"""
example_scenarios= [["aa"], ["b"]]
# Gradio interface setup
with gr.Blocks(css=css) as app:
gr.Markdown("<div id='logo'>J<span>ustEva</span></div>")
gr.Markdown("<h1 style='text-align: center; color: #5f4b8b;'>Gender Bias Analysis Platform</h1>")
gr.Markdown("<p style='text-align: center; font-size: 16px; color: #4a4a4a;'>Powered by AI to advocate against gender-based violence</p>")
with gr.Tab("Text Analysis"):
text_input = gr.Textbox(label="Enter Text or Select an Example", placeholder="Type here or select an example...", lines=4)
analyze_text_btn = gr.Button("Analyze Text")
text_output = gr.Textbox(label="Analysis Output", lines=6)
examples = gr.Examples(
examples=example_scenarios,
inputs=text_input,
outputs=text_output
)
analyze_text_btn.click(
fn=analyze_text,
inputs=text_input,
outputs=text_output
)
app.launch()
|