Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,14 @@ from typing import List, Tuple, Optional
|
|
| 4 |
import google.generativeai as genai
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
-
import tempfile
|
| 8 |
-
import os
|
| 9 |
|
|
|
|
| 10 |
GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 11 |
|
| 12 |
IMAGE_WIDTH = 512
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
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."
|
| 16 |
model_name = "gemini-2.0-flash-exp"
|
| 17 |
-
#model = genai.GenerativeModel(model_name, system_instruction=system_instruction_analysis)
|
| 18 |
-
#genai.configure(api_key=google_key)
|
| 19 |
|
| 20 |
-
# Helper Functions
|
| 21 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
| 22 |
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
| 23 |
|
|
@@ -38,172 +32,85 @@ def bot(
|
|
| 38 |
top_p: float,
|
| 39 |
chatbot: List[Tuple[str, str]]
|
| 40 |
):
|
| 41 |
-
#
|
| 42 |
used_api_key = google_key if google_key else GOOGLE_API_KEY
|
|
|
|
| 43 |
if not used_api_key:
|
| 44 |
-
chatbot[-1][1] = "❌ No API Key found! Please provide it."
|
| 45 |
yield chatbot
|
| 46 |
return
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
|
| 49 |
|
| 50 |
-
# Handle cases for text and/or image input
|
| 51 |
if not text_prompt and not image_prompt:
|
| 52 |
-
chatbot[-1][1] = "Prompt cannot be empty.
|
| 53 |
yield chatbot
|
| 54 |
return
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
# Configure the model
|
| 63 |
-
genai.configure(api_key=used_api_key)
|
| 64 |
-
#generation_config = genai.types.GenerationConfig(
|
| 65 |
-
# temperature=temperature,
|
| 66 |
-
# max_output_tokens=max_output_tokens,
|
| 67 |
-
# stop_sequences=preprocess_stop_sequences(stop_sequences),
|
| 68 |
-
# top_k=top_k,
|
| 69 |
-
# top_p=top_p,
|
| 70 |
-
#)
|
| 71 |
-
model = genai.GenerativeModel(model_name, system_instruction=system_instruction_analysis)
|
| 72 |
-
|
| 73 |
-
# Prepare inputs
|
| 74 |
-
text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
|
| 75 |
-
inputs = [text_prompt] if image_prompt is None else [text_prompt, image_prompt]
|
| 76 |
-
|
| 77 |
-
# Generate response
|
| 78 |
try:
|
|
|
|
| 79 |
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
-
chatbot[-1][1] = f"Error
|
| 83 |
yield chatbot
|
| 84 |
-
return
|
| 85 |
-
|
| 86 |
-
# Stream the response back to the chatbot
|
| 87 |
-
chatbot[-1][1] = ""
|
| 88 |
-
for chunk in response:
|
| 89 |
-
for i in range(0, len(chunk.text), 10):
|
| 90 |
-
chatbot[-1][1] += chunk.text[i:i + 10]
|
| 91 |
-
time.sleep(0.01)
|
| 92 |
-
yield chatbot
|
| 93 |
-
# Components
|
| 94 |
-
google_key_component = gr.Textbox(
|
| 95 |
-
label="Google API Key",
|
| 96 |
-
type="password",
|
| 97 |
-
placeholder="Enter your Google API Key",
|
| 98 |
-
visible=GOOGLE_API_KEY is None
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
image_prompt_component = gr.Image(type="pil", label="Input Image (Optional: Figure/Graph)")
|
| 102 |
-
chatbot_component = gr.Chatbot(label="Chatbot", bubble_full_width=False)
|
| 103 |
-
text_prompt_component = gr.Textbox(
|
| 104 |
-
placeholder="Type your question here...",
|
| 105 |
-
label="Ask",
|
| 106 |
-
lines=3
|
| 107 |
-
)
|
| 108 |
-
run_button_component = gr.Button("Submit")
|
| 109 |
-
temperature_component = gr.Slider(
|
| 110 |
-
minimum=0,
|
| 111 |
-
maximum=1.0,
|
| 112 |
-
value=0.4,
|
| 113 |
-
step=0.05,
|
| 114 |
-
label="Creativity (Temperature)",
|
| 115 |
-
info="Controls the randomness of the response. Higher values result in more creative answers."
|
| 116 |
-
)
|
| 117 |
-
max_output_tokens_component = gr.Slider(
|
| 118 |
-
minimum=1,
|
| 119 |
-
maximum=2048,
|
| 120 |
-
value=1024,
|
| 121 |
-
step=1,
|
| 122 |
-
label="Response Length (Token Limit)",
|
| 123 |
-
info="Sets the maximum number of tokens in the output response."
|
| 124 |
-
)
|
| 125 |
-
stop_sequences_component = gr.Textbox(
|
| 126 |
-
label="Stop Sequences (Optional)",
|
| 127 |
-
placeholder="Enter stop sequences, e.g., STOP, END",
|
| 128 |
-
info="Specify sequences to stop the generation."
|
| 129 |
-
)
|
| 130 |
-
top_k_component = gr.Slider(
|
| 131 |
-
minimum=1,
|
| 132 |
-
maximum=40,
|
| 133 |
-
value=32,
|
| 134 |
-
step=1,
|
| 135 |
-
label="Top-K Sampling",
|
| 136 |
-
info="Limits token selection to the top K most probable tokens. Lower values produce conservative outputs."
|
| 137 |
-
)
|
| 138 |
-
top_p_component = gr.Slider(
|
| 139 |
-
minimum=0,
|
| 140 |
-
maximum=1,
|
| 141 |
-
value=1,
|
| 142 |
-
step=0.01,
|
| 143 |
-
label="Top-P Sampling",
|
| 144 |
-
info="Limits token selection to tokens with a cumulative probability up to P. Lower values produce conservative outputs."
|
| 145 |
-
)
|
| 146 |
-
example_scenarios = [
|
| 147 |
-
"Describe Multimodal AI",
|
| 148 |
-
"What are the difference between muliagent llm and multiagent system",
|
| 149 |
-
"Why it's difficult to intgrate multimodality in prompt"]
|
| 150 |
-
example_images = [["ex1.png"],["ex2.png"]]
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
# Gradio Interface
|
| 154 |
-
user_inputs = [text_prompt_component, chatbot_component]
|
| 155 |
-
bot_inputs = [
|
| 156 |
-
google_key_component,
|
| 157 |
-
image_prompt_component,
|
| 158 |
-
temperature_component,
|
| 159 |
-
max_output_tokens_component,
|
| 160 |
-
stop_sequences_component,
|
| 161 |
-
top_k_component,
|
| 162 |
-
top_p_component,
|
| 163 |
-
chatbot_component,
|
| 164 |
-
]
|
| 165 |
-
|
| 166 |
|
|
|
|
| 167 |
with gr.Blocks(theme="earneleh/paris") as demo:
|
| 168 |
-
gr.Markdown("
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
with gr.Row():
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
label="
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
inputs=[example_radio],
|
| 189 |
-
outputs=[text_prompt_component])
|
| 190 |
-
# Custom examples section with blue styling
|
| 191 |
-
with gr.Accordion("🧪Example Image 🩻", open=False):
|
| 192 |
-
gr.Examples(
|
| 193 |
-
examples=example_images,
|
| 194 |
-
inputs=[image_prompt_component],
|
| 195 |
-
label="Example Figures",
|
| 196 |
-
)
|
| 197 |
-
with gr.Accordion("🛠️Customize", open=False):
|
| 198 |
-
temperature_component.render()
|
| 199 |
-
max_output_tokens_component.render()
|
| 200 |
-
stop_sequences_component.render()
|
| 201 |
-
top_k_component.render()
|
| 202 |
-
top_p_component.render()
|
| 203 |
-
|
| 204 |
-
run_button_component.click(
|
| 205 |
-
fn=user, inputs=user_inputs, outputs=[text_prompt_component, chatbot_component]
|
| 206 |
).then(
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
)
|
|
|
|
| 209 |
demo.launch()
|
|
|
|
| 4 |
import google.generativeai as genai
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 1. محاولة جلب المفتاح من النظام (Secrets)
|
| 9 |
GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 10 |
|
| 11 |
IMAGE_WIDTH = 512
|
| 12 |
+
system_instruction_analysis = "You are an expert of the given topic. Analyze text and images with academic standards."
|
|
|
|
|
|
|
| 13 |
model_name = "gemini-2.0-flash-exp"
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 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 |
|
|
|
|
| 32 |
top_p: float,
|
| 33 |
chatbot: List[Tuple[str, str]]
|
| 34 |
):
|
| 35 |
+
# استخدام المفتاح من الخانة أو من السيكرتس
|
| 36 |
used_api_key = google_key if google_key else GOOGLE_API_KEY
|
| 37 |
+
|
| 38 |
if not used_api_key:
|
| 39 |
+
chatbot[-1][1] = "❌ No API Key found! Please provide it in the textbox or set GEMINI_API_KEY in Secrets."
|
| 40 |
yield chatbot
|
| 41 |
return
|
| 42 |
|
| 43 |
+
# ضبط الإعدادات وتفعيل الموديل داخل الدالة (لضمان الترتيب الصحيح)
|
| 44 |
+
genai.configure(api_key=used_api_key)
|
| 45 |
+
|
| 46 |
+
generation_config = genai.types.GenerationConfig(
|
| 47 |
+
temperature=temperature,
|
| 48 |
+
max_output_tokens=max_output_tokens,
|
| 49 |
+
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
| 50 |
+
top_k=top_k,
|
| 51 |
+
top_p=top_p,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
model = genai.GenerativeModel(model_name, system_instruction=system_instruction_analysis)
|
| 55 |
+
|
| 56 |
text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
|
| 57 |
|
|
|
|
| 58 |
if not text_prompt and not image_prompt:
|
| 59 |
+
chatbot[-1][1] = "Prompt cannot be empty."
|
| 60 |
yield chatbot
|
| 61 |
return
|
| 62 |
+
|
| 63 |
+
# تحضير المدخلات
|
| 64 |
+
inputs = []
|
| 65 |
+
if text_prompt:
|
| 66 |
+
inputs.append(text_prompt)
|
| 67 |
+
if image_prompt:
|
| 68 |
+
inputs.append(preprocess_image(image_prompt))
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
try:
|
| 71 |
+
# توليد الاستجابة مع تفعيل الـ stream
|
| 72 |
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
| 73 |
+
chatbot[-1][1] = ""
|
| 74 |
+
for chunk in response:
|
| 75 |
+
chatbot[-1][1] += chunk.text
|
| 76 |
+
yield chatbot
|
| 77 |
except Exception as e:
|
| 78 |
+
chatbot[-1][1] = f"Error: {str(e)}"
|
| 79 |
yield chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# --- بناء الواجهة ---
|
| 82 |
with gr.Blocks(theme="earneleh/paris") as demo:
|
| 83 |
+
gr.Markdown("# ✨ Gemini 2.0 Multimodal Assistant")
|
| 84 |
+
|
| 85 |
+
# إظهار خانة الـ Key فقط إذا لم يكن موجوداً في السيكرتس
|
| 86 |
+
google_key_component = gr.Textbox(
|
| 87 |
+
label="Google API Key",
|
| 88 |
+
type="password",
|
| 89 |
+
placeholder="Paste your API key here...",
|
| 90 |
+
visible=GOOGLE_API_KEY is None or GOOGLE_API_KEY == ""
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
chatbot_component = gr.Chatbot(label="Conversation", bubble_full_width=False)
|
| 94 |
+
|
| 95 |
with gr.Row():
|
| 96 |
+
text_prompt_component = gr.Textbox(placeholder="Ask anything...", label="Text Input", scale=3)
|
| 97 |
+
image_prompt_component = gr.Image(type="pil", label="Image (Optional)", scale=1)
|
| 98 |
+
run_button = gr.Button("Submit", variant="primary")
|
| 99 |
+
|
| 100 |
+
with gr.Accordion("⚙️ Settings (Temperature, Tokens, etc.)", open=False):
|
| 101 |
+
temp_slider = gr.Slider(0, 1, value=0.4, label="Temperature")
|
| 102 |
+
max_tokens = gr.Slider(256, 4096, value=1024, label="Max Tokens")
|
| 103 |
+
stop_seq = gr.Textbox(label="Stop Sequences")
|
| 104 |
+
top_k_val = gr.Slider(1, 40, value=32, label="Top-K")
|
| 105 |
+
top_p_val = gr.Slider(0, 1, value=1, label="Top-P")
|
| 106 |
+
|
| 107 |
+
# ربط الأحداث
|
| 108 |
+
run_button.click(
|
| 109 |
+
user, [text_prompt_component, chatbot_component], [text_prompt_component, chatbot_component]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
).then(
|
| 111 |
+
bot,
|
| 112 |
+
[google_key_component, image_prompt_component, temp_slider, max_tokens, stop_seq, top_k_val, top_p_val, chatbot_component],
|
| 113 |
+
chatbot_component
|
| 114 |
)
|
| 115 |
+
|
| 116 |
demo.launch()
|