vuminhtue commited on
Commit
d64b570
·
verified ·
1 Parent(s): 3a68aec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -7
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
- # Initialize the Hugging Face Inference Client
5
- # No API key needed for public models, but you can add one for better rate limits
6
- client = InferenceClient()
 
7
 
8
  def chatbot(question, model, temperature):
9
  """Send the question to HuggingFace model using the Inference API.
@@ -29,7 +31,30 @@ def chatbot(question, model, temperature):
29
  return f"Model: {model}\nTemperature: {temperature}\n\n{content}"
30
 
31
  except Exception as e:
32
- return f"Error: {str(e)}\n\nPlease try again or select a different model."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
 
35
  def main():
@@ -38,6 +63,12 @@ def main():
38
  gr.Markdown("# 🤗 HuggingFace Chatbot")
39
  gr.Markdown("Ask questions and get responses from various HuggingFace AI models!")
40
 
 
 
 
 
 
 
41
  with gr.Row():
42
  # Left column: grouped controls (model dropdown + temperature slider)
43
  with gr.Column(scale=1):
@@ -45,12 +76,14 @@ def main():
45
  model_dropdown = gr.Dropdown(
46
  label="Model",
47
  choices=[
 
48
  "meta-llama/Llama-3.2-3B-Instruct",
49
  "microsoft/Phi-3-mini-4k-instruct",
 
50
  "mistralai/Mistral-7B-Instruct-v0.3",
51
- "HuggingFaceH4/zephyr-7b-beta"
52
  ],
53
- value="meta-llama/Llama-3.2-3B-Instruct",
54
  )
55
  temp_slider = gr.Slider(
56
  label="Temperature",
@@ -90,4 +123,3 @@ def main():
90
 
91
  if __name__ == "__main__":
92
  main()
93
-
 
1
  import gradio as gr
2
+ import os
3
  from huggingface_hub import InferenceClient
4
 
5
+ # Initialize the Hugging Face Inference Client with token from environment
6
+ # Get token from HuggingFace Spaces secrets or environment variable
7
+ hf_token = os.environ.get("HF_TOKEN", None)
8
+ client = InferenceClient(token=hf_token)
9
 
10
  def chatbot(question, model, temperature):
11
  """Send the question to HuggingFace model using the Inference API.
 
31
  return f"Model: {model}\nTemperature: {temperature}\n\n{content}"
32
 
33
  except Exception as e:
34
+ error_msg = str(e)
35
+ if "api_key" in error_msg.lower() or "authentication" in error_msg.lower():
36
+ return """⚠️ Authentication Error: This model requires a HuggingFace token.
37
+
38
+ Please follow these steps:
39
+
40
+ 1. Get a FREE token from: https://huggingface.co/settings/tokens
41
+ - Click "New token"
42
+ - Name it (e.g., "chatbot-token")
43
+ - Select "Read" permission
44
+ - Click "Create token" and copy it
45
+
46
+ 2. Add the token to your Space:
47
+ - Go to your Space Settings → "Variables and secrets"
48
+ - Click "New secret"
49
+ - Name: HF_TOKEN
50
+ - Value: [paste your token]
51
+ - Save
52
+
53
+ 3. Restart your Space (it will rebuild automatically)
54
+
55
+ OR try selecting a different model that doesn't require authentication."""
56
+ else:
57
+ return f"Error: {error_msg}\n\nPlease try again or select a different model."
58
 
59
 
60
  def main():
 
63
  gr.Markdown("# 🤗 HuggingFace Chatbot")
64
  gr.Markdown("Ask questions and get responses from various HuggingFace AI models!")
65
 
66
+ # Show token status
67
+ if hf_token:
68
+ gr.Markdown("✅ **Status**: HuggingFace token configured")
69
+ else:
70
+ gr.Markdown("⚠️ **Status**: No HuggingFace token found. Some models may not work. See instructions below if you get errors.")
71
+
72
  with gr.Row():
73
  # Left column: grouped controls (model dropdown + temperature slider)
74
  with gr.Column(scale=1):
 
76
  model_dropdown = gr.Dropdown(
77
  label="Model",
78
  choices=[
79
+ "meta-llama/Llama-3.2-1B-Instruct",
80
  "meta-llama/Llama-3.2-3B-Instruct",
81
  "microsoft/Phi-3-mini-4k-instruct",
82
+ "HuggingFaceH4/zephyr-7b-beta",
83
  "mistralai/Mistral-7B-Instruct-v0.3",
84
+ "google/gemma-2-2b-it"
85
  ],
86
+ value="meta-llama/Llama-3.2-1B-Instruct",
87
  )
88
  temp_slider = gr.Slider(
89
  label="Temperature",
 
123
 
124
  if __name__ == "__main__":
125
  main()