Spaces:
Paused
Paused
Migrate Nightly to Stable
Browse files- Tabs/{Gemini_Chabot_Nightly.py โ Gemini_Chabot_Stable.py} +25 -14
- app.py +6 -225
Tabs/{Gemini_Chabot_Nightly.py โ Gemini_Chabot_Stable.py}
RENAMED
|
@@ -5,22 +5,33 @@ from dotenv import load_dotenv
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
-
|
| 9 |
model_name = "gemini-1.5-flash"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
Notices ๐:
|
| 14 |
-
- This app is still in development
|
| 15 |
- Some features may not work as expected
|
| 16 |
-
- The chatbot supports text, documents, and images
|
| 17 |
"""
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Known errors โ ๏ธ:
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
|
| 23 |
-
genai.configure(api_key=
|
| 24 |
model = genai.GenerativeModel(
|
| 25 |
model_name,
|
| 26 |
safety_settings=[
|
|
@@ -61,7 +72,7 @@ def transform_history(history):
|
|
| 61 |
new_history.append({"role": "model", "parts": [model_msg]})
|
| 62 |
return new_history
|
| 63 |
|
| 64 |
-
def
|
| 65 |
message_text = message["text"]
|
| 66 |
message_files = message["files"]
|
| 67 |
if message_files:
|
|
@@ -75,7 +86,7 @@ def chatbot_nightly(message, history):
|
|
| 75 |
|
| 76 |
return response.text
|
| 77 |
|
| 78 |
-
|
| 79 |
height=400,
|
| 80 |
likeable=True,
|
| 81 |
avatar_images=(
|
|
@@ -88,13 +99,13 @@ gemini_chatbot_interface_nightly = gr.Chatbot(
|
|
| 88 |
)
|
| 89 |
|
| 90 |
clear_chat_button = gr.ClearButton(
|
| 91 |
-
components=[
|
| 92 |
value="๐๏ธ Clear"
|
| 93 |
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
fn=
|
| 97 |
-
chatbot=
|
| 98 |
multimodal=True,
|
| 99 |
clear_btn=clear_chat_button
|
| 100 |
)
|
|
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 9 |
model_name = "gemini-1.5-flash"
|
| 10 |
|
| 11 |
+
TITLE = """<h1 align="center">๐ฎChat with Gemini 1.5๐ฅ -- Beta Preview</h1>"""
|
| 12 |
+
NOTICE = """
|
| 13 |
Notices ๐:
|
| 14 |
+
- This app is still in development
|
| 15 |
- Some features may not work as expected
|
|
|
|
| 16 |
"""
|
| 17 |
+
ABOUT = """
|
| 18 |
+
Updates (2024-8-16): Upgrade UI & chat with documents
|
| 19 |
+
|
| 20 |
+
Info:
|
| 21 |
+
- Model: Gemini 1.5 Flash
|
| 22 |
+
- Chat with Gemini 1.5 Flash model with images and documents
|
| 23 |
+
"""
|
| 24 |
+
ERRORS = """
|
| 25 |
Known errors โ ๏ธ:
|
| 26 |
+
"""
|
| 27 |
+
FUTURE_IMPLEMENTATIONS = """
|
| 28 |
+
To be implemented ๐:
|
| 29 |
+
- Select other Gemini / Gemma models
|
| 30 |
+
- Upload files
|
| 31 |
+
- More tools other than web search
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 35 |
model = genai.GenerativeModel(
|
| 36 |
model_name,
|
| 37 |
safety_settings=[
|
|
|
|
| 72 |
new_history.append({"role": "model", "parts": [model_msg]})
|
| 73 |
return new_history
|
| 74 |
|
| 75 |
+
def chatbot_stable(message, history):
|
| 76 |
message_text = message["text"]
|
| 77 |
message_files = message["files"]
|
| 78 |
if message_files:
|
|
|
|
| 86 |
|
| 87 |
return response.text
|
| 88 |
|
| 89 |
+
gemini_chatbot_interface = gr.Chatbot(
|
| 90 |
height=400,
|
| 91 |
likeable=True,
|
| 92 |
avatar_images=(
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
clear_chat_button = gr.ClearButton(
|
| 102 |
+
components=[gemini_chatbot_interface],
|
| 103 |
value="๐๏ธ Clear"
|
| 104 |
)
|
| 105 |
|
| 106 |
+
gemini_chatbot = gr.ChatInterface(
|
| 107 |
+
fn=chatbot_stable,
|
| 108 |
+
chatbot=gemini_chatbot_interface,
|
| 109 |
multimodal=True,
|
| 110 |
clear_btn=clear_chat_button
|
| 111 |
)
|
app.py
CHANGED
|
@@ -1,186 +1,5 @@
|
|
| 1 |
-
"""
|
| 2 |
-
References:
|
| 3 |
-
- https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
import time
|
| 8 |
-
from typing import List, Tuple, Optional
|
| 9 |
-
import google.generativeai as genai
|
| 10 |
import gradio as gr
|
| 11 |
-
from
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
-
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
| 14 |
-
from Tabs.Gemini_Chabot_Nightly import gemini_chatbot_nightly, clear_chat_button, clear_chat_history, TITLE_NIGHTLY, NOTICE_NIGHTLY, ERROR_NIGHTLY
|
| 15 |
-
|
| 16 |
-
load_dotenv()
|
| 17 |
-
|
| 18 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 19 |
-
|
| 20 |
-
# ============================== Stable - START ==============================
|
| 21 |
-
TITLE = """<h1 align="center">๐ฎChat with Gemini 1.5๐ฅ -- Beta Preview</h1>"""
|
| 22 |
-
NOTICE = """
|
| 23 |
-
Notices ๐:
|
| 24 |
-
- This app is still in development
|
| 25 |
-
- Some features may not work as expected
|
| 26 |
-
"""
|
| 27 |
-
ABOUT = """
|
| 28 |
-
Updates (2024-8-12): Created the App
|
| 29 |
-
|
| 30 |
-
Info:
|
| 31 |
-
- Model: Gemini 1.5 Flash
|
| 32 |
-
"""
|
| 33 |
-
ERRORS = """
|
| 34 |
-
Known errors โ ๏ธ:
|
| 35 |
-
"""
|
| 36 |
-
FUTURE_IMPLEMENTATIONS = """
|
| 37 |
-
To be implemented ๐:
|
| 38 |
-
- Select other Gemini / Gemma models
|
| 39 |
-
- Upload files
|
| 40 |
-
- More tools other than web search
|
| 41 |
-
"""
|
| 42 |
-
IMAGE_WIDTH = 512
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def preprocess_stop_sequences(
|
| 46 |
-
stop_sequences: str
|
| 47 |
-
) -> Optional[List[str]]:
|
| 48 |
-
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def preprocess_image(
|
| 52 |
-
image: Image.Image
|
| 53 |
-
) -> Image.Image:
|
| 54 |
-
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 55 |
-
return image.resize((IMAGE_WIDTH, image_height))
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def user(
|
| 59 |
-
text_prompt: str,
|
| 60 |
-
chatbot: List[Tuple[str, str]]
|
| 61 |
-
):
|
| 62 |
-
return "", chatbot + [[text_prompt, None]]
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def bot(
|
| 66 |
-
google_key: str,
|
| 67 |
-
image_prompt: Optional[Image.Image],
|
| 68 |
-
temperature: float,
|
| 69 |
-
max_output_tokens: int,
|
| 70 |
-
stop_sequences: str,
|
| 71 |
-
top_k: int,
|
| 72 |
-
top_p: float,
|
| 73 |
-
chatbot: List[Tuple[str, str]]
|
| 74 |
-
):
|
| 75 |
-
google_key = google_key or GEMINI_API_KEY
|
| 76 |
-
if not google_key:
|
| 77 |
-
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 78 |
-
|
| 79 |
-
text_prompt = chatbot[-1][0]
|
| 80 |
-
genai.configure(api_key=google_key)
|
| 81 |
-
generation_config = genai.types.GenerationConfig(
|
| 82 |
-
temperature=temperature,
|
| 83 |
-
max_output_tokens=max_output_tokens,
|
| 84 |
-
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
| 85 |
-
top_k=top_k,
|
| 86 |
-
top_p=top_p,
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
model_name = "gemini-1.5-flash"
|
| 90 |
-
model = genai.GenerativeModel(
|
| 91 |
-
model_name,
|
| 92 |
-
safety_settings=[
|
| 93 |
-
{
|
| 94 |
-
"category": "HARM_CATEGORY_HARASSMENT",
|
| 95 |
-
"threshold": "BLOCK_NONE"
|
| 96 |
-
},
|
| 97 |
-
{
|
| 98 |
-
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 99 |
-
"threshold": "BLOCK_NONE"
|
| 100 |
-
},
|
| 101 |
-
{
|
| 102 |
-
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 103 |
-
"threshold": "BLOCK_NONE"
|
| 104 |
-
},
|
| 105 |
-
{
|
| 106 |
-
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 107 |
-
"threshold": "BLOCK_NONE"
|
| 108 |
-
}
|
| 109 |
-
],
|
| 110 |
-
generation_config=generation_config
|
| 111 |
-
)
|
| 112 |
-
inputs = [text_prompt] if image_prompt is None else [
|
| 113 |
-
text_prompt, preprocess_image(image_prompt)
|
| 114 |
-
]
|
| 115 |
-
|
| 116 |
-
response = model.generate_content(
|
| 117 |
-
inputs,
|
| 118 |
-
stream=True
|
| 119 |
-
)
|
| 120 |
-
response.resolve()
|
| 121 |
-
|
| 122 |
-
chatbot[-1][1] = ""
|
| 123 |
-
for chunk in response:
|
| 124 |
-
for i in range(0, len(chunk.text), 10):
|
| 125 |
-
chatbot[-1][1] += chunk.text[i:i + 10]
|
| 126 |
-
time.sleep(0.01)
|
| 127 |
-
yield chatbot
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
google_key_component = gr.Textbox(
|
| 131 |
-
label="GOOGLE API KEY",
|
| 132 |
-
type="password",
|
| 133 |
-
placeholder="...",
|
| 134 |
-
visible=GEMINI_API_KEY is None
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
image_prompt_component = gr.Image(type="pil", label="Image")
|
| 138 |
-
chatbot_component = gr.Chatbot(bubble_full_width=False)
|
| 139 |
-
text_prompt_component = gr.Textbox(
|
| 140 |
-
placeholder="Chat with Gemini",
|
| 141 |
-
label="Ask me anything and press Enter"
|
| 142 |
-
)
|
| 143 |
-
run_button_component = gr.Button("Run")
|
| 144 |
-
temperature_component = gr.Slider(
|
| 145 |
-
minimum=0,
|
| 146 |
-
maximum=1.0,
|
| 147 |
-
value=0.5,
|
| 148 |
-
step=0.05,
|
| 149 |
-
label="Temperature"
|
| 150 |
-
)
|
| 151 |
-
max_output_tokens_component = gr.Slider(
|
| 152 |
-
minimum=1,
|
| 153 |
-
maximum=8192,
|
| 154 |
-
value=4096,
|
| 155 |
-
step=1,
|
| 156 |
-
label="Max Output Tokens"
|
| 157 |
-
)
|
| 158 |
-
stop_sequences_component = gr.Textbox(
|
| 159 |
-
label="Add stop sequence",
|
| 160 |
-
placeholder="STOP, END"
|
| 161 |
-
)
|
| 162 |
-
top_k_component = gr.Slider(
|
| 163 |
-
minimum=1,
|
| 164 |
-
maximum=40,
|
| 165 |
-
value=32,
|
| 166 |
-
step=1,
|
| 167 |
-
label="Top-K"
|
| 168 |
-
)
|
| 169 |
-
top_p_component = gr.Slider(
|
| 170 |
-
minimum=0,
|
| 171 |
-
maximum=1,
|
| 172 |
-
value=1,
|
| 173 |
-
step=0.01,
|
| 174 |
-
label="Top-P"
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
user_inputs = [text_prompt_component, chatbot_component]
|
| 178 |
-
bot_inputs = [
|
| 179 |
-
google_key_component, image_prompt_component, temperature_component,
|
| 180 |
-
max_output_tokens_component, stop_sequences_component, top_k_component,
|
| 181 |
-
top_p_component, chatbot_component
|
| 182 |
-
]
|
| 183 |
-
# ============================== Stable - END ==============================
|
| 184 |
|
| 185 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 186 |
# ============================== Stable - START ==============================
|
|
@@ -191,40 +10,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 191 |
gr.Markdown(ABOUT)
|
| 192 |
gr.Markdown(ERRORS)
|
| 193 |
gr.Markdown(FUTURE_IMPLEMENTATIONS)
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
image_prompt_component.render()
|
| 198 |
-
chatbot_component.render()
|
| 199 |
-
text_prompt_component.render()
|
| 200 |
-
run_button_component.render()
|
| 201 |
-
with gr.Accordion("Parameters", open=False):
|
| 202 |
-
temperature_component.render()
|
| 203 |
-
max_output_tokens_component.render()
|
| 204 |
-
stop_sequences_component.render()
|
| 205 |
-
with gr.Accordion("Advanced", open=False):
|
| 206 |
-
top_k_component.render()
|
| 207 |
-
top_p_component.render()
|
| 208 |
-
|
| 209 |
-
run_button_component.click(
|
| 210 |
-
fn=user,
|
| 211 |
-
inputs=user_inputs,
|
| 212 |
-
outputs=[text_prompt_component, chatbot_component],
|
| 213 |
-
queue=False
|
| 214 |
-
).then(
|
| 215 |
-
fn=bot,
|
| 216 |
-
inputs=bot_inputs,
|
| 217 |
-
outputs=[chatbot_component]
|
| 218 |
-
)
|
| 219 |
-
text_prompt_component.submit(
|
| 220 |
-
fn=user,
|
| 221 |
-
inputs=user_inputs,
|
| 222 |
-
outputs=[text_prompt_component, chatbot_component],
|
| 223 |
-
queue=False
|
| 224 |
-
).then(
|
| 225 |
-
fn=bot,
|
| 226 |
-
inputs=bot_inputs,
|
| 227 |
-
outputs=[chatbot_component]
|
| 228 |
)
|
| 229 |
# ============================== Stable - END ==============================
|
| 230 |
|
|
@@ -233,14 +21,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 233 |
|
| 234 |
# ============================== Nightly - START ==============================
|
| 235 |
with gr.Tab("Nightly -- Chat with Gemini 1.5"):
|
| 236 |
-
gr.HTML(
|
| 237 |
-
|
| 238 |
-
gr.Markdown(NOTICE_NIGHTLY)
|
| 239 |
-
gr.Markdown(ERROR_NIGHTLY)
|
| 240 |
-
gemini_chatbot_nightly.render()
|
| 241 |
-
clear_chat_button.click(
|
| 242 |
-
fn=clear_chat_history
|
| 243 |
-
)
|
| 244 |
-
# ============================== Nightly - END ==============================
|
| 245 |
|
| 246 |
demo.queue().launch(debug=True, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from Tabs.Gemini_Chabot_Stable import gemini_chatbot, clear_chat_button, clear_chat_history, TITLE, NOTICE, ERRORS, FUTURE_IMPLEMENTATIONS, ABOUT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 5 |
# ============================== Stable - START ==============================
|
|
|
|
| 10 |
gr.Markdown(ABOUT)
|
| 11 |
gr.Markdown(ERRORS)
|
| 12 |
gr.Markdown(FUTURE_IMPLEMENTATIONS)
|
| 13 |
+
gemini_chatbot.render()
|
| 14 |
+
clear_chat_button.click(
|
| 15 |
+
fn=clear_chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
# ============================== Stable - END ==============================
|
| 18 |
|
|
|
|
| 21 |
|
| 22 |
# ============================== Nightly - START ==============================
|
| 23 |
with gr.Tab("Nightly -- Chat with Gemini 1.5"):
|
| 24 |
+
gr.HTML("""<h1 align="center">Still in development</h1>""")
|
| 25 |
+
# ============================== Nightly - END ==============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
demo.queue().launch(debug=True, show_error=True)
|