Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
from typing import List, Tuple, Optional
|
4 |
+
|
5 |
+
import google.generativeai as genai
|
6 |
+
import gradio as gr
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
print("google-generativeai:", genai.__version__)
|
10 |
+
|
11 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
12 |
+
|
13 |
+
# UI Titles and Subtitles
|
14 |
+
TITLE = "<h1 align='center'>π Gender Bias Detection App π</h1>"
|
15 |
+
SUBTITLE = "<h2 align='center'>Detect and analyze gender-based discrimination in communication.</h2>"
|
16 |
+
|
17 |
+
IMAGE_WIDTH = 512
|
18 |
+
|
19 |
+
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
20 |
+
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
21 |
+
|
22 |
+
def preprocess_image(image: Image.Image) -> Image.Image:
|
23 |
+
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
24 |
+
return image.resize((IMAGE_WIDTH, image_height))
|
25 |
+
|
26 |
+
def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
27 |
+
return "", chatbot + [[text_prompt, None]]
|
28 |
+
|
29 |
+
def bot(
|
30 |
+
google_key: str,
|
31 |
+
image_prompt: Optional[Image.Image],
|
32 |
+
temperature: float,
|
33 |
+
max_output_tokens: int,
|
34 |
+
stop_sequences: str,
|
35 |
+
top_k: int,
|
36 |
+
top_p: float,
|
37 |
+
chatbot: List[Tuple[str, str]]
|
38 |
+
):
|
39 |
+
google_key = google_key or GOOGLE_API_KEY
|
40 |
+
if not google_key:
|
41 |
+
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
42 |
+
|
43 |
+
text_prompt = chatbot[-1][0]
|
44 |
+
genai.configure(api_key=google_key)
|
45 |
+
generation_config = genai.types.GenerationConfig(
|
46 |
+
temperature=temperature,
|
47 |
+
max_output_tokens=max_output_tokens,
|
48 |
+
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
49 |
+
top_k=top_k,
|
50 |
+
top_p=top_p,
|
51 |
+
instructions="Analyze this text for gender-based discrimination, including implicit biases and stereotypes. Provide specific examples and explain why each example demonstrates bias. Also, suggest tips for how to address or mitigate these biases within the text."
|
52 |
+
)
|
53 |
+
|
54 |
+
model_name = "gemini-1.5-pro-latest"
|
55 |
+
model = genai.GenerativeModel(model_name)
|
56 |
+
inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
|
57 |
+
|
58 |
+
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
59 |
+
response.resolve()
|
60 |
+
|
61 |
+
chatbot[-1][1] = ""
|
62 |
+
for chunk in response:
|
63 |
+
for i in range(0, len(chunk.text), 10):
|
64 |
+
chatbot[-1][1] += chunk.text[i:i + 10]
|
65 |
+
time.sleep(0.01)
|
66 |
+
yield chatbot
|
67 |
+
|
68 |
+
# Gradio Interface
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown(TITLE)
|
71 |
+
gr.Markdown(SUBTITLE)
|
72 |
+
with gr.Row():
|
73 |
+
text_input = gr.Textbox(placeholder="Enter text to analyze for gender-based discrimination, label="Text Input")
|
74 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
75 |
+
submit_button = gr.Button("Analyze")
|
76 |
+
chatbot_output = gr.Chatbot(label="Analysis Output")
|
77 |
+
|
78 |
+
submit_button.click(
|
79 |
+
fn=bot,
|
80 |
+
inputs=[GOOGLE_API_KEY, image_input, 0.4, 1024, "END", 32, 1, chatbot_output],
|
81 |
+
outputs=[chatbot_output]
|
82 |
+
)
|
83 |
+
|
84 |
+
demo.launch()
|