mimifuel2018 commited on
Commit
538a10c
·
verified ·
1 Parent(s): a60db2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -34
app.py CHANGED
@@ -1,16 +1,13 @@
1
  import os
2
  import gradio as gr
3
- from http import HTTPStatus
4
  from typing import List, Optional, Tuple, Dict
5
  import dashscope
6
  from dashscope import Generation
7
  from dashscope.api_entities.dashscope_response import Role
8
- import requests # <-- Add this line to import the requests library
9
-
10
 
11
  # Configuration
12
  default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
13
- dashscope.api_key = os.getenv('HF_TOKEN') # Replace 'YOUR_API_TOKEN' with your actual API token.
14
 
15
  # Typing definitions
16
  History = List[Tuple[str, str]]
@@ -55,46 +52,37 @@ def model_chat(query: Optional[str], history: Optional[History], system: str) ->
55
  query = ''
56
  if history is None:
57
  history = []
58
-
59
- # Ensure the query is clearly asking for numbers
60
- if 'next numbers' in query or 'give me numbers after' in query:
61
- query = "Please give me the next 10 numbers after 10, starting from 11."
62
 
63
  messages = history_to_messages(history, system)
64
- messages.append({'role': 'user', 'content': query})
65
-
66
- payload = {"inputs": query, "parameters": {"max_new_tokens": 150}, "history": messages}
67
- headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
68
 
69
  try:
70
- response = requests.post(f"https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
71
- json=payload, headers=headers)
 
 
 
 
72
 
73
- if response.status_code == 200:
74
- response_data = response.json()
75
-
76
- if isinstance(response_data, list):
77
- response_text = response_data[0].get('generated_text', '')
 
 
 
 
 
78
  else:
79
- response_text = response_data.get('generated_text', '')
80
-
81
- # Log the chat to file
82
- log_history_to_file(query, response_text)
83
-
84
- # Update history with the new assistant response and return it
85
- history.append([query, response_text])
86
- return response_text, history, system
87
- else:
88
- error_message = f"Error {response.status_code}: {response.json().get('error', response.text)}"
89
- log_history_to_file(query, error_message)
90
- return error_message, history, system
91
  except Exception as e:
92
  error_message = f"Exception: {str(e)}"
93
  log_history_to_file(query, error_message)
94
  return error_message, history, system
95
 
96
-
97
-
98
  # Gradio Interface Setup
99
  with gr.Blocks() as demo:
100
  gr.Markdown("<center><font size=8>Qwen2.5-72B-Instruct👾</center>")
@@ -113,7 +101,6 @@ with gr.Blocks() as demo:
113
  clear_history = gr.Button("🧹 Clear history")
114
  submit = gr.Button("🚀 Send")
115
 
116
- # Link buttons to functions
117
  textbox.submit(model_chat,
118
  inputs=[textbox, chatbot, system_state],
119
  outputs=[textbox, chatbot, system_input],
 
1
  import os
2
  import gradio as gr
 
3
  from typing import List, Optional, Tuple, Dict
4
  import dashscope
5
  from dashscope import Generation
6
  from dashscope.api_entities.dashscope_response import Role
 
 
7
 
8
  # Configuration
9
  default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'
10
+ dashscope.api_key = os.getenv('HF_TOKEN')
11
 
12
  # Typing definitions
13
  History = List[Tuple[str, str]]
 
52
  query = ''
53
  if history is None:
54
  history = []
 
 
 
 
55
 
56
  messages = history_to_messages(history, system)
57
+ messages.append({'role': Role.USER, 'content': query})
 
 
 
58
 
59
  try:
60
+ gen = Generation.call(
61
+ model='qwen2.5-72b-instruct',
62
+ messages=messages,
63
+ result_format='message',
64
+ stream=True
65
+ )
66
 
67
+ for response in gen:
68
+ if response.status_code == 200:
69
+ response_text = response.output.choices[0].message.content
70
+
71
+ # Log the chat to file
72
+ log_history_to_file(query, response_text)
73
+
74
+ # Update history with the new assistant response
75
+ history.append((query, response_text))
76
+ return response_text, history, system
77
  else:
78
+ error_message = f"Error: {response.status_code} - {response.message}"
79
+ log_history_to_file(query, error_message)
80
+ return error_message, history, system
 
 
 
 
 
 
 
 
 
81
  except Exception as e:
82
  error_message = f"Exception: {str(e)}"
83
  log_history_to_file(query, error_message)
84
  return error_message, history, system
85
 
 
 
86
  # Gradio Interface Setup
87
  with gr.Blocks() as demo:
88
  gr.Markdown("<center><font size=8>Qwen2.5-72B-Instruct👾</center>")
 
101
  clear_history = gr.Button("🧹 Clear history")
102
  submit = gr.Button("🚀 Send")
103
 
 
104
  textbox.submit(model_chat,
105
  inputs=[textbox, chatbot, system_state],
106
  outputs=[textbox, chatbot, system_input],