Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829import gradio as grimport requestsimport osHF_TOKEN = os.getenv("HF_TOKEN")MODEL = os.getenv("HF_MODEL", "TinyLlama/TinyLlama-1.1B-Chat-v1.0")def chat(message):headers = {"Authorization": f"Bearer {HF_TOKEN}"}payload = {"inputs": message}response = requests.post(f"https://api-inference.huggingface.co/models/{MODEL}",headers=headers,json=payload)if response.status_code != 200:return f"⚠️ Error desde Hugging Face: {response.text}"result = response.json()if isinstance(result, list) and len(result) > 0:return result[0].get("generated_text", "")elif isinstance(result, dict) and "generated_text" in result:return result["generated_text"]else:return str(result)gr.ChatInterface(fn=chat, title="Chat Gradio con HF").launch()123456789101112131415161718192021222324252627282930313233import gradio as grfrom huggingface_hub import InferenceClient# 🔑 Tu modelo gratuito y cliente HFMODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"client = InferenceClient(model=MODEL_ID)def responder(mensaje, historial):try:respuesta = ""for salida in client.chat_completion(model=MODEL_ID,messages=[{"role": "user", "content": mensaje}],max_tokens=200,stream=True,):if salida.choices[0].delta.content:respuesta += salida.choices[0].delta.contenthistorial.append((mensaje, respuesta))return "", historialexcept Exception as e:return f"⚠️ Error interno: {e}", historial# 🎨 Interfaz de Gradiodemo = gr.ChatInterface(responder,title="Chat Gradio con HF (TinyLlama)",description="Modelo gratuito usando TinyLlama desde Hugging Face Inference API",theme="soft",)if __name__ == "__main__":demo.launch()
app.py+28-24