Myloiose commited on
Commit
2ac7db4
·
verified ·
1 Parent(s): f904d53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -1,29 +1,33 @@
1
  import gradio as gr
2
- import requests
3
- import os
4
 
5
- HF_TOKEN = os.getenv("HF_TOKEN")
6
- MODEL = os.getenv("HF_MODEL", "TinyLlama/TinyLlama-1.1B-Chat-v1.0")
 
7
 
8
- def chat(message):
9
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
10
- payload = {"inputs": message}
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- response = requests.post(
13
- f"https://api-inference.huggingface.co/models/{MODEL}",
14
- headers=headers,
15
- json=payload
16
- )
 
 
17
 
18
- if response.status_code != 200:
19
- return f"⚠️ Error desde Hugging Face: {response.text}"
20
-
21
- result = response.json()
22
- if isinstance(result, list) and len(result) > 0:
23
- return result[0].get("generated_text", "")
24
- elif isinstance(result, dict) and "generated_text" in result:
25
- return result["generated_text"]
26
- else:
27
- return str(result)
28
-
29
- gr.ChatInterface(fn=chat, title="Chat Gradio con HF").launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
3
 
4
+ # 🔑 Tu modelo gratuito y cliente HF
5
+ MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
6
+ client = InferenceClient(model=MODEL_ID)
7
 
8
+ def responder(mensaje, historial):
9
+ try:
10
+ respuesta = ""
11
+ for salida in client.chat_completion(
12
+ model=MODEL_ID,
13
+ messages=[{"role": "user", "content": mensaje}],
14
+ max_tokens=200,
15
+ stream=True,
16
+ ):
17
+ if salida.choices[0].delta.content:
18
+ respuesta += salida.choices[0].delta.content
19
+ historial.append((mensaje, respuesta))
20
+ return "", historial
21
+ except Exception as e:
22
+ return f"⚠️ Error interno: {e}", historial
23
 
24
+ # 🎨 Interfaz de Gradio
25
+ demo = gr.ChatInterface(
26
+ responder,
27
+ title="Chat Gradio con HF (TinyLlama)",
28
+ description="Modelo gratuito usando TinyLlama desde Hugging Face Inference API",
29
+ theme="soft",
30
+ )
31
 
32
+ if __name__ == "__main__":
33
+ demo.launch()