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