File size: 4,524 Bytes
d22b8a9
d64b570
d22b8a9
 
d64b570
 
 
 
d22b8a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d64b570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d22b8a9
 
 
 
 
 
 
 
d64b570
 
 
 
 
 
d22b8a9
 
 
 
 
 
 
d64b570
d22b8a9
 
d64b570
d22b8a9
d64b570
d22b8a9
d64b570
d22b8a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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()