Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,15 +6,10 @@ from typing import List, Tuple, Optional, Dict, Union
|
|
6 |
import google.generativeai as genai
|
7 |
import gradio as gr
|
8 |
from PIL import Image
|
9 |
-
from dotenv import load_dotenv
|
10 |
|
11 |
print("google-generativeai:", genai.__version__)
|
12 |
|
13 |
-
|
14 |
-
load_dotenv()
|
15 |
-
|
16 |
-
# Configurar la API de Google usando la clave desde las variables de entorno
|
17 |
-
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
18 |
|
19 |
TITLE = """<h1 align="center">Gemini Playground 💬</h1>"""
|
20 |
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
@@ -62,6 +57,7 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
62 |
return "", chatbot
|
63 |
|
64 |
def bot(
|
|
|
65 |
files: Optional[List[str]],
|
66 |
temperature: float,
|
67 |
max_output_tokens: int,
|
@@ -70,11 +66,10 @@ def bot(
|
|
70 |
top_p: float,
|
71 |
chatbot: CHAT_HISTORY
|
72 |
):
|
73 |
-
|
74 |
-
|
75 |
-
raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
|
76 |
|
77 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
78 |
generation_config = genai.types.GenerationConfig(
|
79 |
temperature=temperature,
|
80 |
max_output_tokens=max_output_tokens,
|
@@ -97,12 +92,19 @@ def bot(
|
|
97 |
time.sleep(0.01)
|
98 |
yield chatbot
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
chatbot_component = gr.Chatbot(
|
101 |
label='Gemini',
|
102 |
bubble_full_width=False,
|
103 |
scale=2,
|
104 |
-
height=
|
105 |
-
type="messages" # Agregado para solucionar la advertencia
|
106 |
)
|
107 |
text_prompt_component = gr.Textbox(
|
108 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
@@ -152,6 +154,7 @@ user_inputs = [
|
|
152 |
]
|
153 |
|
154 |
bot_inputs = [
|
|
|
155 |
upload_button_component,
|
156 |
temperature_component,
|
157 |
max_output_tokens_component,
|
@@ -166,6 +169,7 @@ with gr.Blocks() as demo:
|
|
166 |
gr.HTML(SUBTITLE)
|
167 |
gr.HTML(DES)
|
168 |
with gr.Column():
|
|
|
169 |
chatbot_component.render()
|
170 |
with gr.Row():
|
171 |
text_prompt_component.render()
|
@@ -180,13 +184,13 @@ with gr.Blocks() as demo:
|
|
180 |
top_p_component.render()
|
181 |
|
182 |
run_button_component.click(
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
).then(
|
188 |
-
|
189 |
-
)
|
190 |
|
191 |
text_prompt_component.submit(
|
192 |
fn=user,
|
|
|
6 |
import google.generativeai as genai
|
7 |
import gradio as gr
|
8 |
from PIL import Image
|
|
|
9 |
|
10 |
print("google-generativeai:", genai.__version__)
|
11 |
|
12 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
|
|
|
|
|
13 |
|
14 |
TITLE = """<h1 align="center">Gemini Playground 💬</h1>"""
|
15 |
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
|
|
57 |
return "", chatbot
|
58 |
|
59 |
def bot(
|
60 |
+
google_key: str,
|
61 |
files: Optional[List[str]],
|
62 |
temperature: float,
|
63 |
max_output_tokens: int,
|
|
|
66 |
top_p: float,
|
67 |
chatbot: CHAT_HISTORY
|
68 |
):
|
69 |
+
if not google_key and not GOOGLE_API_KEY:
|
70 |
+
raise ValueError("GOOGLE_API_KEY is not set.")
|
|
|
71 |
|
72 |
+
genai.configure(api_key=google_key if google_key else GOOGLE_API_KEY)
|
73 |
generation_config = genai.types.GenerationConfig(
|
74 |
temperature=temperature,
|
75 |
max_output_tokens=max_output_tokens,
|
|
|
92 |
time.sleep(0.01)
|
93 |
yield chatbot
|
94 |
|
95 |
+
google_key_component = gr.Textbox(
|
96 |
+
label="GOOGLE API KEY",
|
97 |
+
value="",
|
98 |
+
type="password",
|
99 |
+
placeholder="...",
|
100 |
+
info="Please provide your own GOOGLE_API_KEY for this app",
|
101 |
+
visible=GOOGLE_API_KEY is None
|
102 |
+
)
|
103 |
chatbot_component = gr.Chatbot(
|
104 |
label='Gemini',
|
105 |
bubble_full_width=False,
|
106 |
scale=2,
|
107 |
+
height=600
|
|
|
108 |
)
|
109 |
text_prompt_component = gr.Textbox(
|
110 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
|
|
154 |
]
|
155 |
|
156 |
bot_inputs = [
|
157 |
+
google_key_component,
|
158 |
upload_button_component,
|
159 |
temperature_component,
|
160 |
max_output_tokens_component,
|
|
|
169 |
gr.HTML(SUBTITLE)
|
170 |
gr.HTML(DES)
|
171 |
with gr.Column():
|
172 |
+
google_key_component.render()
|
173 |
chatbot_component.render()
|
174 |
with gr.Row():
|
175 |
text_prompt_component.render()
|
|
|
184 |
top_p_component.render()
|
185 |
|
186 |
run_button_component.click(
|
187 |
+
fn=user,
|
188 |
+
inputs=user_inputs,
|
189 |
+
outputs=[text_prompt_component, chatbot_component],
|
190 |
+
queue=False
|
191 |
+
).then(
|
192 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
|
193 |
+
)
|
194 |
|
195 |
text_prompt_component.submit(
|
196 |
fn=user,
|