Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
OLLAMA_MODEL = "qwen:0.5b"
|
| 7 |
+
OLLAMA_URL = "http://localhost:11434/api/generate"
|
| 8 |
+
|
| 9 |
+
# Función para verificar si Ollama está corriendo
|
| 10 |
+
def check_ollama():
|
| 11 |
+
try:
|
| 12 |
+
response = requests.get("http://localhost:11434", timeout=5)
|
| 13 |
+
return response.status_code == 200
|
| 14 |
+
except:
|
| 15 |
+
return False
|
| 16 |
+
|
| 17 |
+
# Función para enviar mensaje al modelo
|
| 18 |
+
def chat_with_qwen(message, history):
|
| 19 |
+
if not check_ollama():
|
| 20 |
+
return "⚠️ Ollama no está corriendo. Asegúrate de que el modelo esté cargado."
|
| 21 |
+
|
| 22 |
+
messages = [{"role": "user", "content": msg} for _, msg in history] + [{"role": "user", "content": message}]
|
| 23 |
+
|
| 24 |
+
payload = {
|
| 25 |
+
"model": OLLAMA_MODEL,
|
| 26 |
+
"prompt": message,
|
| 27 |
+
"stream": False
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(OLLAMA_URL, json=payload, timeout=60)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
return response.json().get("response", "Sin respuesta")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error al comunicarse con Ollama: {e}"
|
| 36 |
+
|
| 37 |
+
# Interfaz Gradio
|
| 38 |
+
iface = gr.ChatInterface(
|
| 39 |
+
fn=chat_with_qwen,
|
| 40 |
+
title="Chat con Qwen 0.5B (Ollama)",
|
| 41 |
+
description="Chatea con el modelo Qwen 0.5B usando Ollama en Hugging Face Spaces.",
|
| 42 |
+
theme="soft",
|
| 43 |
+
examples=["¿Qué es Python?", "Explícame la teoría de cuerdas."],
|
| 44 |
+
cache_examples=False
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|