Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,11 +7,22 @@ import gradio as gr
|
|
7 |
from PIL import Image
|
8 |
|
9 |
print("google-generativeai:", genai.__version__)
|
|
|
10 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
IMAGE_WIDTH = 512
|
16 |
|
17 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
@@ -25,6 +36,7 @@ def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
|
25 |
return "", chatbot + [[text_prompt, None]]
|
26 |
|
27 |
def bot(
|
|
|
28 |
image_prompt: Optional[Image.Image],
|
29 |
temperature: float,
|
30 |
max_output_tokens: int,
|
@@ -33,27 +45,28 @@ def bot(
|
|
33 |
top_p: float,
|
34 |
chatbot: List[Tuple[str, str]]
|
35 |
):
|
36 |
-
|
|
|
37 |
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
38 |
-
|
39 |
text_prompt = chatbot[-1][0]
|
40 |
-
genai.configure(api_key=
|
41 |
generation_config = genai.types.GenerationConfig(
|
42 |
temperature=temperature,
|
43 |
max_output_tokens=max_output_tokens,
|
44 |
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
45 |
top_k=top_k,
|
46 |
top_p=top_p,
|
47 |
-
instructions="
|
48 |
)
|
49 |
-
|
|
|
50 |
model = genai.GenerativeModel(model_name)
|
51 |
-
|
52 |
-
# Correctly handle inputs based on image_prompt
|
53 |
inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
|
54 |
|
55 |
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
56 |
response.resolve()
|
|
|
57 |
chatbot[-1][1] = ""
|
58 |
for chunk in response:
|
59 |
for i in range(0, len(chunk.text), 10):
|
@@ -61,15 +74,45 @@ def bot(
|
|
61 |
time.sleep(0.01)
|
62 |
yield chatbot
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
with gr.Blocks() as demo:
|
66 |
-
gr.
|
67 |
-
gr.
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
TITLE = """<h1 align="center">🕹️ Google Gemini Chatbot 🔥</h1>"""
|
14 |
+
SUBTITLE = """<h2 align="center">🎨Create with Multimodal Gemini</h2>"""
|
15 |
+
DUPLICATE = """
|
16 |
+
<div style="text-align: center; display: flex; justify-content: center; align-items: center;">
|
17 |
+
<a href="https://huggingface.co/spaces/Rahatara/build_with_gemini/blob/main/allgemapp.py?duplicate=true">
|
18 |
+
<img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
|
19 |
+
</a>
|
20 |
+
<span>Duplicate the Space and run securely with your
|
21 |
+
<a href="https://makersuite.google.com/app/apikey">GOOGLE API KEY</a>.
|
22 |
+
</span>
|
23 |
+
</div>
|
24 |
+
"""
|
25 |
+
|
26 |
IMAGE_WIDTH = 512
|
27 |
|
28 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
|
|
36 |
return "", chatbot + [[text_prompt, None]]
|
37 |
|
38 |
def bot(
|
39 |
+
google_key: str,
|
40 |
image_prompt: Optional[Image.Image],
|
41 |
temperature: float,
|
42 |
max_output_tokens: int,
|
|
|
45 |
top_p: float,
|
46 |
chatbot: List[Tuple[str, str]]
|
47 |
):
|
48 |
+
google_key = google_key or GOOGLE_API_KEY
|
49 |
+
if not google_key:
|
50 |
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
51 |
+
|
52 |
text_prompt = chatbot[-1][0]
|
53 |
+
genai.configure(api_key=google_key)
|
54 |
generation_config = genai.types.GenerationConfig(
|
55 |
temperature=temperature,
|
56 |
max_output_tokens=max_output_tokens,
|
57 |
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
58 |
top_k=top_k,
|
59 |
top_p=top_p,
|
60 |
+
instructions = "You are an advocate against gender-based discrimination "
|
61 |
)
|
62 |
+
|
63 |
+
model_name = "gemini-1.5-pro-latest"
|
64 |
model = genai.GenerativeModel(model_name)
|
|
|
|
|
65 |
inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
|
66 |
|
67 |
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
68 |
response.resolve()
|
69 |
+
|
70 |
chatbot[-1][1] = ""
|
71 |
for chunk in response:
|
72 |
for i in range(0, len(chunk.text), 10):
|
|
|
74 |
time.sleep(0.01)
|
75 |
yield chatbot
|
76 |
|
77 |
+
google_key_component = gr.Textbox(
|
78 |
+
label="GOOGLE API KEY",
|
79 |
+
type="password",
|
80 |
+
placeholder="...",
|
81 |
+
visible=GOOGLE_API_KEY is None
|
82 |
+
)
|
83 |
+
|
84 |
+
image_prompt_component = gr.Image(type="pil", label="Image")
|
85 |
+
chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False)
|
86 |
+
text_prompt_component = "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"
|
87 |
+
run_button_component = gr.Button("Check Discrimantion")
|
88 |
+
temperature_component = gr.Slider(minimum=0, maximum=1.0, value=0.4, step=0.05, label="Temperature")
|
89 |
+
max_output_tokens_component = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Token limit")
|
90 |
+
stop_sequences_component = gr.Textbox(label="Add stop sequence", placeholder="STOP, END")
|
91 |
+
top_k_component = gr.Slider(minimum=1, maximum=40, value=32, step=1, label="Top-K")
|
92 |
+
top_p_component = gr.Slider(minimum=0, maximum=1, value=1, step=0.01, label="Top-P")
|
93 |
+
|
94 |
+
user_inputs = [text_prompt_component, chatbot_component]
|
95 |
+
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]
|
96 |
+
|
97 |
with gr.Blocks() as demo:
|
98 |
+
gr.HTML(TITLE)
|
99 |
+
gr.HTML(SUBTITLE)
|
100 |
+
gr.HTML(DUPLICATE)
|
101 |
+
with gr.Column():
|
102 |
+
google_key_component.render()
|
103 |
+
with gr.Row():
|
104 |
+
image_prompt_component.render()
|
105 |
+
chatbot_component.render()
|
106 |
+
text_prompt_component.render()
|
107 |
+
run_button_component.render()
|
108 |
+
with gr.Accordion("Parameters", open=False):
|
109 |
+
temperature_component.render()
|
110 |
+
max_output_tokens_component.render()
|
111 |
+
stop_sequences_component.render()
|
112 |
+
with gr.Accordion("Advanced", open=False):
|
113 |
+
top_k_component.render()
|
114 |
+
top_p_component.render()
|
115 |
+
|
116 |
+
run_button_component.click(fn=user, inputs=user_inputs, outputs=[text_prompt_component, chatbot_component], queue=False).then(fn=bot, inputs=bot_inputs, outputs=[chatbot_component])
|
117 |
+
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])
|
118 |
+
demo.launch()
|