chathf / app.py
vuminhtue's picture
Update app.py
d64b570 verified
import gradio as gr
import os
from huggingface_hub import InferenceClient
# Initialize the Hugging Face Inference Client with token from environment
# Get token from HuggingFace Spaces secrets or environment variable
hf_token = os.environ.get("HF_TOKEN", None)
client = InferenceClient(token=hf_token)
def chatbot(question, model, temperature):
"""Send the question to HuggingFace model using the Inference API.
This function sends your question to a HuggingFace model and returns the response.
"""
if not question:
return "Please enter a question."
try:
# Call HuggingFace Inference API with the selected model
response = client.chat_completion(
model=model,
messages=[{"role": "user", "content": question}],
temperature=float(temperature),
max_tokens=500
)
# Extract the response content
content = response.choices[0].message.content
# Include model and temperature information in the returned text
return f"Model: {model}\nTemperature: {temperature}\n\n{content}"
except Exception as e:
error_msg = str(e)
if "api_key" in error_msg.lower() or "authentication" in error_msg.lower():
return """⚠️ Authentication Error: This model requires a HuggingFace token.
Please follow these steps:
1. Get a FREE token from: https://huggingface.co/settings/tokens
- Click "New token"
- Name it (e.g., "chatbot-token")
- Select "Read" permission
- Click "Create token" and copy it
2. Add the token to your Space:
- Go to your Space Settings → "Variables and secrets"
- Click "New secret"
- Name: HF_TOKEN
- Value: [paste your token]
- Save
3. Restart your Space (it will rebuild automatically)
OR try selecting a different model that doesn't require authentication."""
else:
return f"Error: {error_msg}\n\nPlease try again or select a different model."
def main():
# Build a simple Gradio app with a left-side control panel (model + temperature)
with gr.Blocks(title="HuggingFace Chatbot") as demo:
gr.Markdown("# 🤗 HuggingFace Chatbot")
gr.Markdown("Ask questions and get responses from various HuggingFace AI models!")
# Show token status
if hf_token:
gr.Markdown("✅ **Status**: HuggingFace token configured")
else:
gr.Markdown("⚠️ **Status**: No HuggingFace token found. Some models may not work. See instructions below if you get errors.")
with gr.Row():
# Left column: grouped controls (model dropdown + temperature slider)
with gr.Column(scale=1):
gr.Markdown("**Model & Settings**")
model_dropdown = gr.Dropdown(
label="Model",
choices=[
"meta-llama/Llama-3.2-1B-Instruct",
"meta-llama/Llama-3.2-3B-Instruct",
"microsoft/Phi-3-mini-4k-instruct",
"HuggingFaceH4/zephyr-7b-beta",
"mistralai/Mistral-7B-Instruct-v0.3",
"google/gemma-2-2b-it"
],
value="meta-llama/Llama-3.2-1B-Instruct",
)
temp_slider = gr.Slider(
label="Temperature",
minimum=0.0,
maximum=1.0,
step=0.01,
value=0.7,
info="Higher values make output more random"
)
# Right column: input and output
with gr.Column(scale=3):
question = gr.Textbox(
label="Question",
lines=3,
placeholder="Type your message here..."
)
submit = gr.Button("Submit", variant="primary")
output = gr.Textbox(label="Response", lines=15)
# Wire up the button to call the chatbot function with model and temperature
submit.click(
fn=chatbot,
inputs=[question, model_dropdown, temp_slider],
outputs=output
)
# Also allow pressing Enter to submit
question.submit(
fn=chatbot,
inputs=[question, model_dropdown, temp_slider],
outputs=output
)
demo.launch()
if __name__ == "__main__":
main()