Nhughes09 commited on
Commit
6484aef
·
1 Parent(s): cdfd669

Use HF_TOKEN from environment secrets instead of hardcoded

Browse files
Files changed (1) hide show
  1. app.py +36 -56
app.py CHANGED
@@ -3,11 +3,11 @@ import requests
3
  import logging
4
  import sys
5
  import time
 
6
 
7
- # --- Extreme Logging Setup ---
8
  logging.basicConfig(
9
  level=logging.INFO,
10
- format='%(asctime)s - %(levelname)s - %(message)s',
11
  handlers=[logging.StreamHandler(sys.stdout)]
12
  )
13
  logger = logging.getLogger("ChatbotBrain")
@@ -15,98 +15,77 @@ logger = logging.getLogger("ChatbotBrain")
15
  logger.info(f"Gradio Version: {gr.__version__}")
16
  logger.info(f"Python Version: {sys.version}")
17
 
18
- # --- Configuration ---
19
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
20
- HF_TOKEN = "hf_" + "tHMFjUJIvQEMMSxyYZiNshryJqKagoUQBL"
21
 
22
- # New API endpoint (api-inference is deprecated)
 
 
 
 
 
 
23
  API_URL = f"https://router.huggingface.co/hf-inference/models/{MODEL_NAME}"
24
- HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
25
 
26
  logger.info(f"Using Model: {MODEL_NAME}")
27
  logger.info(f"API URL: {API_URL}")
28
 
29
- def format_prompt(message, history):
30
- """Formats the conversation history into a prompt for the model."""
31
- prompt = ""
32
- for user_msg, assistant_msg in history:
33
- prompt += f"<|user|>\n{user_msg}</s>\n"
34
- prompt += f"<|assistant|>\n{assistant_msg}</s>\n"
35
- prompt += f"<|user|>\n{message}</s>\n<|assistant|>\n"
36
- return prompt
37
-
38
  def query_model(payload):
39
- """Sends a request to the HF Inference API."""
40
- logger.info(f"Sending request to API with payload: {payload}")
41
- start_time = time.time()
42
-
43
  try:
44
  response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
45
- duration = time.time() - start_time
46
- logger.info(f"API Response Status: {response.status_code} (took {duration:.2f}s)")
47
- logger.info(f"API Response Headers: {dict(response.headers)}")
48
-
49
  if response.status_code != 200:
50
  logger.error(f"API Error: {response.text}")
51
- return {"error": f"API returned status {response.status_code}: {response.text}"}
52
-
53
  result = response.json()
54
- logger.info(f"API Response Body: {result}")
55
  return result
56
- except requests.exceptions.Timeout:
57
- logger.error("API request timed out after 60 seconds")
58
- return {"error": "Request timed out. The model may be loading, please try again."}
59
  except Exception as e:
60
- logger.error(f"Exception during API call: {e}")
61
  return {"error": str(e)}
62
 
63
  def respond(message, history):
64
- """Generates a response from the AI model."""
65
  logger.info("="*50)
66
- logger.info(f"RECEIVED USER MESSAGE: {message}")
67
- logger.info(f"Current History Length: {len(history)}")
68
 
69
- formatted_prompt = format_prompt(message, history)
70
- logger.info(f"Formatted Prompt:\n{formatted_prompt}")
71
- logger.info("Thinking... (Sending request to HF Inference API)")
 
 
 
 
 
 
 
72
 
73
  payload = {
74
- "inputs": formatted_prompt,
75
- "parameters": {
76
- "max_new_tokens": 512,
77
- "temperature": 0.7,
78
- "do_sample": True,
79
- "return_full_text": False
80
- }
81
  }
82
 
83
  result = query_model(payload)
84
 
85
  if "error" in result:
86
- error_msg = result["error"]
87
- logger.error(f"Error from API: {error_msg}")
88
- return f"Error: {error_msg}"
89
 
90
- # Handle different response formats
91
  if isinstance(result, list) and len(result) > 0:
92
- generated_text = result[0].get("generated_text", "")
93
- elif isinstance(result, dict):
94
- generated_text = result.get("generated_text", str(result))
95
  else:
96
- generated_text = str(result)
97
 
98
- logger.info(f"GENERATED RESPONSE:\n{generated_text}")
99
  logger.info("="*50)
100
-
101
- return generated_text
102
 
103
- # --- Gradio UI ---
104
  logger.info("Building Gradio Interface...")
105
 
106
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
107
  gr.Markdown("# CPU Chatbot")
108
  gr.Markdown(f"### Powered by {MODEL_NAME}")
109
- gr.Markdown("Check the **Container Logs** to see the AI 'thinking'!")
110
 
111
  chatbot = gr.Chatbot(height=500)
112
  msg = gr.Textbox(placeholder="Ask me anything...", label="Your message")
@@ -133,3 +112,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
133
  if __name__ == "__main__":
134
  logger.info("Launching Gradio App...")
135
  demo.launch()
 
 
3
  import logging
4
  import sys
5
  import time
6
+ import os
7
 
 
8
  logging.basicConfig(
9
  level=logging.INFO,
10
+ format="%(asctime)s - %(levelname)s - %(message)s",
11
  handlers=[logging.StreamHandler(sys.stdout)]
12
  )
13
  logger = logging.getLogger("ChatbotBrain")
 
15
  logger.info(f"Gradio Version: {gr.__version__}")
16
  logger.info(f"Python Version: {sys.version}")
17
 
 
18
  MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
 
19
 
20
+ HF_TOKEN = os.environ.get("HF_TOKEN")
21
+ if HF_TOKEN:
22
+ logger.info("SUCCESS: HF_TOKEN loaded from environment secrets!")
23
+ logger.info(f"Token starts with: {HF_TOKEN[:10]}...")
24
+ else:
25
+ logger.error("ERROR: HF_TOKEN not found! Add it to Space Secrets.")
26
+
27
  API_URL = f"https://router.huggingface.co/hf-inference/models/{MODEL_NAME}"
28
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
29
 
30
  logger.info(f"Using Model: {MODEL_NAME}")
31
  logger.info(f"API URL: {API_URL}")
32
 
 
 
 
 
 
 
 
 
 
33
  def query_model(payload):
34
+ logger.info(f"Sending request with payload: {payload}")
 
 
 
35
  try:
36
  response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
37
+ logger.info(f"Response Status: {response.status_code}")
 
 
 
38
  if response.status_code != 200:
39
  logger.error(f"API Error: {response.text}")
40
+ return {"error": f"API returned {response.status_code}: {response.text}"}
 
41
  result = response.json()
42
+ logger.info(f"Response Body: {result}")
43
  return result
 
 
 
44
  except Exception as e:
45
+ logger.error(f"Exception: {e}")
46
  return {"error": str(e)}
47
 
48
  def respond(message, history):
 
49
  logger.info("="*50)
50
+ logger.info(f"USER MESSAGE: {message}")
51
+ logger.info(f"History Length: {len(history)}")
52
 
53
+ prompt = ""
54
+ for user_msg, assistant_msg in history:
55
+ prompt += f"User: {user_msg}
56
+ Assistant: {assistant_msg}
57
+ "
58
+ prompt += f"User: {message}
59
+ Assistant:"
60
+
61
+ logger.info(f"Formatted Prompt:
62
+ {prompt}")
63
 
64
  payload = {
65
+ "inputs": prompt,
66
+ "parameters": {"max_new_tokens": 512, "temperature": 0.7, "do_sample": True}
 
 
 
 
 
67
  }
68
 
69
  result = query_model(payload)
70
 
71
  if "error" in result:
72
+ return f"Error: {result['error']}"
 
 
73
 
 
74
  if isinstance(result, list) and len(result) > 0:
75
+ text = result[0].get("generated_text", "")
 
 
76
  else:
77
+ text = str(result)
78
 
79
+ logger.info(f"RESPONSE: {text}")
80
  logger.info("="*50)
81
+ return text
 
82
 
 
83
  logger.info("Building Gradio Interface...")
84
 
85
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
  gr.Markdown("# CPU Chatbot")
87
  gr.Markdown(f"### Powered by {MODEL_NAME}")
88
+ gr.Markdown("Check Container Logs to see AI thinking!")
89
 
90
  chatbot = gr.Chatbot(height=500)
91
  msg = gr.Textbox(placeholder="Ask me anything...", label="Your message")
 
112
  if __name__ == "__main__":
113
  logger.info("Launching Gradio App...")
114
  demo.launch()
115
+