w1r4 commited on
Commit
fbee7ec
·
verified ·
1 Parent(s): f726ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -6,35 +6,32 @@ from huggingface_hub import InferenceClient
6
  model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
7
 
8
  def respond(message, history, system_message, temperature, request: gr.Request):
 
9
  token = None
10
-
11
- # 1. Safely try to get the user's token (works on HF Spaces if logged in)
12
  if request:
13
- # We use getattr() to avoid the "AttributeError" if running locally
14
  token = getattr(request, "token", None)
15
 
16
- # 2. If no user token, fall back to the Space's secret token (HF_TOKEN)
17
  if token is None:
18
  token = os.getenv("HF_TOKEN")
19
 
20
- # 3. If still no token, stop.
21
  if token is None:
22
- yield "Error: No authentication token found. Please add 'HF_TOKEN' to Space Secrets or run locally with an env variable."
23
  return
24
 
25
- # Initialize client
26
  client = InferenceClient(model_id, token=token)
27
 
28
- # Build the message format
29
  messages = [{"role": "system", "content": system_message}]
30
 
31
- # 'history' comes as tuples [(user, bot), (user, bot)...]
32
- for user_msg, bot_msg in history:
33
- messages.append({"role": "user", "content": user_msg})
34
- messages.append({"role": "assistant", "content": bot_msg})
35
 
36
  messages.append({"role": "user", "content": message})
37
 
 
38
  try:
39
  stream = client.chat_completion(
40
  messages,
@@ -43,15 +40,22 @@ def respond(message, history, system_message, temperature, request: gr.Request):
43
  temperature=temperature,
44
  top_p=0.9
45
  )
 
46
  response_text = ""
47
  for chunk in stream:
 
 
 
 
48
  content = chunk.choices[0].delta.content
49
  if content:
50
  response_text += content
51
  yield response_text
 
52
  except Exception as e:
53
  yield f"Error: {str(e)}"
54
 
 
55
  with gr.Blocks(fill_height=True) as demo:
56
  with gr.Sidebar():
57
  gr.Markdown("# AI Coding Assistant")
@@ -60,6 +64,8 @@ with gr.Blocks(fill_height=True) as demo:
60
 
61
  gr.ChatInterface(
62
  respond,
 
 
63
  additional_inputs=[
64
  gr.Textbox(value="You are a helpful assistant.", label="System Instruction", lines=2),
65
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
 
6
  model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
7
 
8
  def respond(message, history, system_message, temperature, request: gr.Request):
9
+ # --- 1. Authentication Logic ---
10
  token = None
11
+ # Safely access token (handles both Local run and Spaces)
 
12
  if request:
 
13
  token = getattr(request, "token", None)
14
 
 
15
  if token is None:
16
  token = os.getenv("HF_TOKEN")
17
 
 
18
  if token is None:
19
+ yield "Error: No authentication token found. Please add 'HF_TOKEN' to Space Secrets."
20
  return
21
 
22
+ # --- 2. Setup Client ---
23
  client = InferenceClient(model_id, token=token)
24
 
25
+ # --- 3. Build Messages (handling history correctly) ---
26
  messages = [{"role": "system", "content": system_message}]
27
 
28
+ # We use type="messages" in ChatInterface, so history is already a list of dicts
29
+ for msg in history:
30
+ messages.append(msg)
 
31
 
32
  messages.append({"role": "user", "content": message})
33
 
34
+ # --- 4. Generate Response ---
35
  try:
36
  stream = client.chat_completion(
37
  messages,
 
40
  temperature=temperature,
41
  top_p=0.9
42
  )
43
+
44
  response_text = ""
45
  for chunk in stream:
46
+ # FIX: Check if choices exist before accessing index [0]
47
+ if not chunk.choices:
48
+ continue
49
+
50
  content = chunk.choices[0].delta.content
51
  if content:
52
  response_text += content
53
  yield response_text
54
+
55
  except Exception as e:
56
  yield f"Error: {str(e)}"
57
 
58
+ # --- 5. Build UI ---
59
  with gr.Blocks(fill_height=True) as demo:
60
  with gr.Sidebar():
61
  gr.Markdown("# AI Coding Assistant")
 
64
 
65
  gr.ChatInterface(
66
  respond,
67
+ # 'type="messages"' fixes the deprecation warning and makes parsing easier
68
+ type="messages",
69
  additional_inputs=[
70
  gr.Textbox(value="You are a helpful assistant.", label="System Instruction", lines=2),
71
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")