Myloiose commited on
Commit
77fdb6e
·
verified ·
1 Parent(s): 9d43069

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,33 +1,32 @@
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()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Modelo gratuito y público
5
+ MODEL_ID = "google/gemma-2b-it"
6
  client = InferenceClient(model=MODEL_ID)
7
 
8
+ def chat(message, history):
9
+ history = history or []
10
+ # Convierte el historial a formato texto
11
+ prompt = ""
12
+ for user_msg, bot_msg in history:
13
+ prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
14
+ prompt += f"User: {message}\nAssistant:"
15
+
16
  try:
17
+ response = ""
18
+ for chunk in client.text_generation(prompt, max_new_tokens=128, stream=True):
19
+ response += chunk.token.text
20
+ history.append((message, response.strip()))
21
+ return history, history
 
 
 
 
 
 
22
  except Exception as e:
23
+ return history + [(message, f"⚠️ Error interno: {e}")], history
24
 
25
+ chatbot = gr.ChatInterface(
26
+ fn=chat,
27
+ title="Chat FastAPI con HF",
28
+ description="Chat simple usando google/gemma-2b-it en Hugging Face"
 
 
29
  )
30
 
31
  if __name__ == "__main__":
32
+ chatbot.launch()