-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot_ollama_gradio.py
More file actions
49 lines (40 loc) · 1.65 KB
/
chatbot_ollama_gradio.py
File metadata and controls
49 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
import requests
# Configura el modelo que estás usando en Ollama
OLLAMA_MODEL = "llama3"
OLLAMA_URL = "http://localhost:11434/api/chat"
# Función que llama a Ollama
def conversar_con_ollama(mensaje, historial):
if historial is None:
historial = []
messages = [{"role": "system", "content": "Eres un asistente útil."}]
for entrada, salida in historial:
messages.append({"role": "user", "content": entrada})
messages.append({"role": "assistant", "content": salida})
messages.append({"role": "user", "content": mensaje})
response = requests.post(OLLAMA_URL, json={
"model": OLLAMA_MODEL,
"messages": messages,
"stream": False
})
if response.status_code == 200:
respuesta = response.json()["message"]["content"]
historial.append((mensaje, respuesta))
return respuesta, historial
else:
return "Error al conectar con Ollama", historial
# Interfaz con Gradio
with gr.Blocks() as interfaz:
gr.Markdown("## 🤖 Chatbot con Ollama + Gradio")
chatbot = gr.Chatbot()
mensaje = gr.Textbox(label="Tu mensaje:")
estado = gr.State([])
def responder(mensaje_usuario, historial):
respuesta, historial_actualizado = conversar_con_ollama(mensaje_usuario, historial)
#return chatbot.update(value=historial_actualizado), historial_actualizado
return historial_actualizado, historial_actualizado
enviar = gr.Button("Enviar")
enviar.click(fn=responder, inputs=[mensaje, estado], outputs=[chatbot, estado])
mensaje.submit(responder, [mensaje, estado], [chatbot, estado])
# Ejecutar interfaz
interfaz.launch()