Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,13 @@ import google.generativeai as genai
|
|
5 |
import gradio as gr
|
6 |
from PIL import Image
|
7 |
|
|
|
8 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
9 |
|
|
|
10 |
IMAGE_WIDTH = 512
|
11 |
|
|
|
12 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
13 |
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
14 |
|
@@ -16,85 +19,61 @@ def preprocess_image(image: Image.Image) -> Image.Image:
|
|
16 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
17 |
return image.resize((IMAGE_WIDTH, image_height))
|
18 |
|
19 |
-
|
20 |
-
return "", chatbot + [[text_prompt, None]]
|
21 |
-
|
22 |
def bot(
|
23 |
google_key: str,
|
24 |
image_prompt: Optional[Image.Image],
|
25 |
-
temperature: float,
|
26 |
-
max_output_tokens: int,
|
27 |
-
stop_sequences: str,
|
28 |
-
top_k: int,
|
29 |
-
top_p: float,
|
30 |
chatbot: List[Tuple[str, str]]
|
31 |
):
|
32 |
google_key = google_key or GOOGLE_API_KEY
|
33 |
if not google_key:
|
34 |
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
|
|
|
|
|
|
35 |
|
36 |
-
text_prompt = chatbot[-1][0]
|
37 |
genai.configure(api_key=google_key)
|
38 |
generation_config = genai.types.GenerationConfig(
|
39 |
-
temperature=
|
40 |
-
max_output_tokens=
|
41 |
-
stop_sequences=preprocess_stop_sequences(
|
42 |
-
top_k=
|
43 |
-
top_p=
|
44 |
-
system_instructions = "You are an advocate against gender-based discrimination "
|
45 |
)
|
46 |
|
47 |
model_name = "gemini-1.5-pro-latest"
|
48 |
model = genai.GenerativeModel(model_name)
|
49 |
-
inputs = [text_prompt] if image_prompt
|
50 |
|
51 |
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
52 |
response.resolve()
|
53 |
|
54 |
chatbot[-1][1] = ""
|
55 |
for chunk in response:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
yield chatbot
|
60 |
-
|
61 |
-
google_key_component = gr.Textbox(
|
62 |
-
label="GOOGLE API KEY",
|
63 |
-
type="password",
|
64 |
-
placeholder="...",
|
65 |
-
visible=GOOGLE_API_KEY is None
|
66 |
-
)
|
67 |
|
68 |
-
|
|
|
|
|
69 |
chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False)
|
70 |
-
|
71 |
-
text_prompt_component = gr.Textbox(placeholder="Hi there!", label="Ask me anything and press Enter")
|
72 |
-
run_button_component = gr.Button("Check Discrimantion")
|
73 |
-
temperature_component = gr.Slider(minimum=0, maximum=1.0, value=0.4, step=0.05, label="Temperature")
|
74 |
-
max_output_tokens_component = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Token limit")
|
75 |
-
stop_sequences_component = gr.Textbox(label="Add stop sequence", placeholder="STOP, END")
|
76 |
-
top_k_component = gr.Slider(minimum=1, maximum=40, value=32, step=1, label="Top-K")
|
77 |
-
top_p_component = gr.Slider(minimum=0, maximum=1, value=1, step=0.01, label="Top-P")
|
78 |
-
|
79 |
-
user_inputs = [text_prompt_component, chatbot_component]
|
80 |
-
bot_inputs = [google_key_component, image_prompt_component, temperature_component, max_output_tokens_component, stop_sequences_component, top_k_component, top_p_component, chatbot_component]
|
81 |
|
|
|
82 |
with gr.Blocks() as demo:
|
83 |
with gr.Column():
|
84 |
google_key_component.render()
|
85 |
-
|
86 |
-
image_prompt_component.render()
|
87 |
-
chatbot_component.render()
|
88 |
-
text_prompt_component.render()
|
89 |
run_button_component.render()
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
97 |
|
98 |
-
|
99 |
-
text_prompt_component.submit(fn=user, inputs=user_inputs, outputs=[text_prompt_component, chatbot_component], queue=False).then(fn=bot, inputs=bot_inputs, outputs=[chatbot_component])
|
100 |
demo.launch()
|
|
|
5 |
import gradio as gr
|
6 |
from PIL import Image
|
7 |
|
8 |
+
# Environment variable for Google API Key
|
9 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
10 |
|
11 |
+
# Constants for image processing
|
12 |
IMAGE_WIDTH = 512
|
13 |
|
14 |
+
# Helper functions
|
15 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
16 |
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
17 |
|
|
|
19 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
20 |
return image.resize((IMAGE_WIDTH, image_height))
|
21 |
|
22 |
+
# Function to trigger model invocation
|
|
|
|
|
23 |
def bot(
|
24 |
google_key: str,
|
25 |
image_prompt: Optional[Image.Image],
|
|
|
|
|
|
|
|
|
|
|
26 |
chatbot: List[Tuple[str, str]]
|
27 |
):
|
28 |
google_key = google_key or GOOGLE_API_KEY
|
29 |
if not google_key:
|
30 |
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
31 |
+
|
32 |
+
# Fixed text prompt for analyzing gender-based discrimination
|
33 |
+
text_prompt = "Analyze this for any instances of gender-based discrimination. Consider both explicit and implicit biases, stereotypes, and unequal treatment. Provide specific examples from the text to support your analysis."
|
34 |
|
|
|
35 |
genai.configure(api_key=google_key)
|
36 |
generation_config = genai.types.GenerationConfig(
|
37 |
+
temperature=0.4,
|
38 |
+
max_output_tokens=1024,
|
39 |
+
stop_sequences=preprocess_stop_sequences("STOP, END"),
|
40 |
+
top_k=32,
|
41 |
+
top_p=1.0,
|
|
|
42 |
)
|
43 |
|
44 |
model_name = "gemini-1.5-pro-latest"
|
45 |
model = genai.GenerativeModel(model_name)
|
46 |
+
inputs = [text_prompt, preprocess_image(image_prompt)] if image_prompt else [text_prompt]
|
47 |
|
48 |
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
49 |
response.resolve()
|
50 |
|
51 |
chatbot[-1][1] = ""
|
52 |
for chunk in response:
|
53 |
+
chatbot[-1][1] += chunk.text
|
54 |
+
time.sleep(0.01)
|
55 |
+
return chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# Gradio Interface Components
|
58 |
+
google_key_component = gr.Textbox(label="GOOGLE API KEY", type="password", placeholder="...", visible=GOOGLE_API_KEY is None)
|
59 |
+
image_prompt_component = gr.Image(type="pil", label="Image", optional=True)
|
60 |
chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False)
|
61 |
+
run_button_component = gr.Button("Check Discrimination")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Layout and Interaction
|
64 |
with gr.Blocks() as demo:
|
65 |
with gr.Column():
|
66 |
google_key_component.render()
|
67 |
+
image_prompt_component.render()
|
|
|
|
|
|
|
68 |
run_button_component.render()
|
69 |
+
chatbot_component.render()
|
70 |
+
|
71 |
+
# Connect button to the bot function
|
72 |
+
run_button_component.click(
|
73 |
+
fn=bot,
|
74 |
+
inputs=[google_key_component, image_prompt_component, chatbot_component],
|
75 |
+
outputs=[chatbot_component]
|
76 |
+
)
|
77 |
|
78 |
+
# Launch the app
|
|
|
79 |
demo.launch()
|