Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,19 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
-
import google.generativeai as genai
|
4 |
import gradio as gr
|
5 |
-
from dotenv import load_dotenv
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
"
|
18 |
-
"
|
19 |
-
"
|
20 |
-
"
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
model = genai.GenerativeModel(
|
26 |
-
model_name="gemini-1.5-flash",
|
27 |
-
generation_config=generation_config,
|
28 |
-
)
|
29 |
-
|
30 |
-
# Inicializar la sesi贸n de chat
|
31 |
-
chat = model.start_chat(history=[])
|
32 |
-
|
33 |
-
# Funci贸n para transformar el historial de Gradio al formato de Gemini
|
34 |
-
def transform_history(history):
|
35 |
-
new_history = []
|
36 |
-
for chat in history:
|
37 |
-
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
38 |
-
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
39 |
-
return new_history
|
40 |
-
|
41 |
-
# Funci贸n de respuesta que maneja el texto y los archivos multimodales
|
42 |
-
def response(message, history):
|
43 |
-
global chat
|
44 |
-
|
45 |
-
# Transformar el historial al formato esperado por Gemini
|
46 |
-
chat.history = transform_history(history)
|
47 |
-
|
48 |
-
# Enviar el mensaje al modelo y obtener la respuesta
|
49 |
-
response = chat.send_message(message["text"])
|
50 |
-
response.resolve()
|
51 |
-
|
52 |
-
# Mostrar la respuesta car谩cter por car谩cter
|
53 |
-
for i in range(len(response.text)):
|
54 |
-
time.sleep(0.01)
|
55 |
-
yield response.text[: i + 1]
|
56 |
-
|
57 |
-
# Funci贸n para manejar los archivos cargados y las im谩genes
|
58 |
-
def count_images(message, history):
|
59 |
-
num_images = len(message["files"]) # Contar las im谩genes cargadas en el mensaje actual
|
60 |
-
total_images = 0
|
61 |
-
for msg in history: # Contar todas las im谩genes en el historial
|
62 |
-
if isinstance(msg["content"], tuple): # Si el contenido es una tupla, es un archivo
|
63 |
-
total_images += 1
|
64 |
-
return f"You just uploaded {num_images} images, total uploaded: {total_images + num_images}"
|
65 |
-
|
66 |
-
# Crear la interfaz de Gradio
|
67 |
-
demo = gr.ChatInterface(
|
68 |
-
response, # Funci贸n de chat para manejar texto y archivos
|
69 |
-
examples=[ # Ejemplos iniciales de mensajes
|
70 |
-
{"text": "No files", "files": []}
|
71 |
-
],
|
72 |
-
multimodal=True, # Activar la modalidad multimodal
|
73 |
-
textbox=gr.MultimodalTextbox( # Configuraci贸n del cuadro de texto multimodal
|
74 |
-
file_count="multiple", # Permitir m煤ltiples archivos
|
75 |
-
file_types=["image"], # Aceptar solo im谩genes
|
76 |
-
sources=["upload", "microphone"] # Fuentes de entrada: carga de archivos y micr贸fono
|
77 |
-
)
|
78 |
-
)
|
79 |
-
|
80 |
-
# Iniciar la interfaz
|
81 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
+
def yes_man(message, history):
|
4 |
+
if message.endswith("?"):
|
5 |
+
return "Yes"
|
6 |
+
else:
|
7 |
+
return "Ask me anything!"
|
8 |
+
|
9 |
+
gr.ChatInterface(
|
10 |
+
yes_man,
|
11 |
+
type="messages",
|
12 |
+
chatbot=gr.Chatbot(height=300),
|
13 |
+
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
|
14 |
+
title="Yes Man",
|
15 |
+
description="Ask Yes Man any question",
|
16 |
+
theme="ocean",
|
17 |
+
examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
|
18 |
+
cache_examples=True,
|
19 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|