w1r4 commited on
Commit
837be14
·
verified ·
1 Parent(s): 146c824

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -1,26 +1,33 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
- # Using Qwen 2.5 Coder
5
  model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
6
 
7
- def respond(message, history, system_message, temperature):
8
- # Initialize the client
9
- client = InferenceClient(model_id)
 
 
10
 
11
- # 1. Start with the System Message
12
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
13
 
14
- # 2. Add the history
15
  for user_msg, bot_msg in history:
16
  messages.append({"role": "user", "content": user_msg})
17
  messages.append({"role": "assistant", "content": bot_msg})
18
 
19
- # 3. Add the current user message
20
  messages.append({"role": "user", "content": message})
21
 
22
- # Generate response
23
- response_text = ""
24
  try:
25
  stream = client.chat_completion(
26
  messages,
@@ -29,38 +36,27 @@ def respond(message, history, system_message, temperature):
29
  temperature=temperature,
30
  top_p=0.9
31
  )
 
32
  for chunk in stream:
33
  content = chunk.choices[0].delta.content
34
  if content:
35
  response_text += content
36
  yield response_text
37
  except Exception as e:
38
- yield f"Error: {str(e)}. The model might be busy."
39
 
40
- # Build the UI
41
  with gr.Blocks(fill_height=True) as demo:
42
  with gr.Sidebar():
43
  gr.Markdown("# AI Coding Assistant")
44
  gr.Markdown(f"Running **{model_id}**")
 
45
  gr.LoginButton("Sign in")
46
 
47
  gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
50
- # System Message Input
51
- gr.Textbox(
52
- value="You are a helpful assistant.",
53
- label="System Instruction",
54
- lines=2
55
- ),
56
- # Temperature Slider
57
- gr.Slider(
58
- minimum=0.1,
59
- maximum=2.0,
60
- value=0.7,
61
- step=0.1,
62
- label="Temperature"
63
- )
64
  ]
65
  )
66
 
 
1
  import gradio as gr
2
+ import os
3
  from huggingface_hub import InferenceClient
4
 
 
5
  model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
6
 
7
+ def respond(message, history, system_message, temperature, request: gr.Request):
8
+ # 1. Try to get the user's token (if they logged in via the button)
9
+ token = None
10
+ if request:
11
+ token = request.token
12
 
13
+ # 2. If no user token, fall back to the Space's secret token (HF_TOKEN)
14
+ if token is None:
15
+ token = os.getenv("HF_TOKEN")
16
+
17
+ if token is None:
18
+ yield "Error: No authentication token found. Please add 'HF_TOKEN' to Space Secrets."
19
+ return
20
+
21
+ # Initialize client with the found token
22
+ client = InferenceClient(model_id, token=token)
23
 
24
+ messages = [{"role": "system", "content": system_message}]
25
  for user_msg, bot_msg in history:
26
  messages.append({"role": "user", "content": user_msg})
27
  messages.append({"role": "assistant", "content": bot_msg})
28
 
 
29
  messages.append({"role": "user", "content": message})
30
 
 
 
31
  try:
32
  stream = client.chat_completion(
33
  messages,
 
36
  temperature=temperature,
37
  top_p=0.9
38
  )
39
+ response_text = ""
40
  for chunk in stream:
41
  content = chunk.choices[0].delta.content
42
  if content:
43
  response_text += content
44
  yield response_text
45
  except Exception as e:
46
+ yield f"Error: {str(e)}"
47
 
 
48
  with gr.Blocks(fill_height=True) as demo:
49
  with gr.Sidebar():
50
  gr.Markdown("# AI Coding Assistant")
51
  gr.Markdown(f"Running **{model_id}**")
52
+ # This button allows users to use their OWN token if they want
53
  gr.LoginButton("Sign in")
54
 
55
  gr.ChatInterface(
56
  respond,
57
  additional_inputs=[
58
+ gr.Textbox(value="You are a helpful assistant.", label="System Instruction", lines=2),
59
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
 
 
 
 
 
 
 
 
 
 
 
 
60
  ]
61
  )
62