|
|
import gradio as gr |
|
|
import os |
|
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
response = client.chat_completion( |
|
|
model=model, |
|
|
messages=[{"role": "user", "content": question}], |
|
|
temperature=float(temperature), |
|
|
max_tokens=500 |
|
|
) |
|
|
|
|
|
|
|
|
content = response.choices[0].message.content |
|
|
|
|
|
|
|
|
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(): |
|
|
|
|
|
with gr.Blocks(title="HuggingFace Chatbot") as demo: |
|
|
gr.Markdown("# 🤗 HuggingFace Chatbot") |
|
|
gr.Markdown("Ask questions and get responses from various HuggingFace AI models!") |
|
|
|
|
|
|
|
|
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(): |
|
|
|
|
|
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" |
|
|
) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
submit.click( |
|
|
fn=chatbot, |
|
|
inputs=[question, model_dropdown, temp_slider], |
|
|
outputs=output |
|
|
) |
|
|
|
|
|
|
|
|
question.submit( |
|
|
fn=chatbot, |
|
|
inputs=[question, model_dropdown, temp_slider], |
|
|
outputs=output |
|
|
) |
|
|
|
|
|
demo.launch() |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|