Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#este archivo carga bien el modelo de llm
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Obt茅n el token de manera segura desde el entorno
|
| 7 |
+
hf_token = os.getenv("HF_API_TOKEN")
|
| 8 |
+
client = InferenceClient(
|
| 9 |
+
"microsoft/Phi-3-mini-4k-instruct",
|
| 10 |
+
token=hf_token
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Define la funci贸n de inferencia que usa la API
|
| 14 |
+
def generate_response(input_text):
|
| 15 |
+
prompt = f"Debes de responder a cualquier pregunta:\nPregunta: {input_text}"
|
| 16 |
+
try:
|
| 17 |
+
# Realizar la inferencia usando el cliente de Hugging Face
|
| 18 |
+
messages = [{"role": "user", "content": prompt}]
|
| 19 |
+
response = client.chat_completion(messages=messages, max_tokens=500)
|
| 20 |
+
|
| 21 |
+
# Extrae el texto generado
|
| 22 |
+
if hasattr(response, 'choices') and response.choices:
|
| 23 |
+
generated_text = response.choices[0].message.content
|
| 24 |
+
else:
|
| 25 |
+
generated_text = str(response)
|
| 26 |
+
|
| 27 |
+
return generated_text
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error al realizar la inferencia: {e}"
|
| 30 |
+
|
| 31 |
+
# Configura la interfaz en Gradio
|
| 32 |
+
demo = gr.Interface(fn=generate_response, inputs="text", outputs="text", title="LLM Chatbot con API de Inferencia")
|
| 33 |
+
demo.launch()
|