Google_Gemini_0 / app.py
Manar11's picture
Update app.py
2734a36 verified
import os
import time
from typing import List, Tuple, Optional
import google.generativeai as genai
import gradio as gr
from PIL import Image
# API KEY (ENVIRONMENT VARIABLE ONLY – NO HARDCODING)
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") or "AIzaSyAx65ry4WsiWdd9Ixh-jsPfx4Lg20NZ5sY"
if not GOOGLE_API_KEY:
raise ValueError("GOOGLE_API_KEY is not set. Add it as an environment variable or HF Secret.")
genai.configure(api_key=GOOGLE_API_KEY)
# MODEL CONFIGURATION
IMAGE_WIDTH = 512
system_instruction_analysis = (
"You are an expert of the given topic. Analyze the provided text with a focus on the topic, "
"identifying recent issues, recent insights, or improvements relevant to academic standards "
"and effectiveness. Offer actionable advice for enhancing knowledge and suggest real-life examples."
)
# Supported public multimodal model
MODEL_NAME = "gemini-3-flash-preview"
model = genai.GenerativeModel(
MODEL_NAME,
system_instruction=system_instruction_analysis
)
# HELPER FUNCTIONS
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
def preprocess_image(image: Image.Image) -> Image.Image:
image_height = int(image.height * IMAGE_WIDTH / image.width)
return image.resize((IMAGE_WIDTH, image_height))
def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
return "", chatbot + [[text_prompt, None]]
def bot(
image_prompt: Optional[Image.Image],
temperature: float,
max_output_tokens: int,
stop_sequences: str,
top_k: int,
top_p: float,
chatbot: List[Tuple[str, str]]
):
text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
if not text_prompt and not image_prompt:
chatbot[-1][1] = "Prompt cannot be empty. Please enter text or upload an image."
yield chatbot
return
if image_prompt and not text_prompt:
text_prompt = "Describe the image."
elif image_prompt and text_prompt:
text_prompt = f"{text_prompt}. Also analyze the provided image."
generation_config = genai.types.GenerationConfig(
temperature=temperature,
max_output_tokens=max_output_tokens,
stop_sequences=preprocess_stop_sequences(stop_sequences),
top_k=top_k,
top_p=top_p,
)
inputs = (
[text_prompt]
if image_prompt is None
else [text_prompt, preprocess_image(image_prompt)]
)
try:
response = model.generate_content(
inputs,
generation_config=generation_config
)
chatbot[-1][1] = response.text
yield chatbot
except Exception as e:
chatbot[-1][1] = f" Error: {str(e)}"
yield chatbot
# UI COMPONENTS
chatbot_component = gr.Chatbot(label="Chatbot", bubble_full_width=False)
text_prompt_component = gr.Textbox(
label="Ask",
placeholder="Type your question here...",
lines=3
)
image_prompt_component = gr.Image(
type="pil",
label="Input Image (Optional: Figure / Graph)"
)
run_button_component = gr.Button("Submit")
temperature_component = gr.Slider(
0, 1.0, 0.4, 0.05, label="Creativity (Temperature)"
)
max_output_tokens_component = gr.Slider(
1, 2048, 1024, 1, label="Response Length (Token Limit)"
)
stop_sequences_component = gr.Textbox(
label="Stop Sequences (Optional)",
placeholder="STOP, END"
)
top_k_component = gr.Slider(
1, 40, 32, 1, label="Top-K Sampling"
)
top_p_component = gr.Slider(
0, 1, 1, 0.01, label="Top-P Sampling"
)
example_scenarios = [
"Describe Multimodal AI",
"What is the difference between multi-agent LLMs and multi-agent systems?",
"Why is it difficult to integrate multimodality in prompts?"
]
example_images = [["ex1.png"], ["ex2.png"]]
# GRADIO APP
with gr.Blocks(theme="earneleh/paris") as demo:
gr.Markdown(
"<h1 style='font-size:36px; font-weight:bold;'>Google Gemini</h1>"
)
chatbot_component.render()
with gr.Row():
text_prompt_component.render()
image_prompt_component.render()
run_button_component.render()
with gr.Accordion("🧪 Example Text 💬", open=False):
example_radio = gr.Radio(
choices=example_scenarios,
label="Example Queries"
)
example_radio.change(
fn=lambda q: q,
inputs=[example_radio],
outputs=[text_prompt_component]
)
with gr.Accordion("🧪 Example Image 🩻", open=False):
gr.Examples(
examples=example_images,
inputs=[image_prompt_component],
label="Example Figures"
)
with gr.Accordion("🛠️ Customize", open=False):
temperature_component.render()
max_output_tokens_component.render()
stop_sequences_component.render()
top_k_component.render()
top_p_component.render()
run_button_component.click(
fn=user,
inputs=[text_prompt_component, chatbot_component],
outputs=[text_prompt_component, chatbot_component]
).then(
fn=bot,
inputs=[
image_prompt_component,
temperature_component,
max_output_tokens_component,
stop_sequences_component,
top_k_component,
top_p_component,
chatbot_component,
],
outputs=[chatbot_component]
)
demo.launch(share=True, debug=True)