littlebird13 commited on
Commit
fbbbd2b
1 Parent(s): b3babbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -29
app.py CHANGED
@@ -12,14 +12,13 @@ from urllib3.exceptions import HTTPError
12
 
13
  from pathlib import Path
14
 
15
- #os.environ['DASHSCOPE_HTTP_BASE_URL'] = 'https://dashscope.aliyuncs.com/api/v1'
 
16
 
17
  import dashscope
18
  from dashscope import MultiModalConversation
19
  API_KEY = os.environ['API_KEY']
20
- BASE_URL = os.environ['DASHSCOPE_HTTP_BASE_URL']
21
  dashscope.api_key = API_KEY
22
- dashscope.base_http_api_url = BASE_URL
23
 
24
  is_modelscope_studio = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'
25
 
@@ -70,30 +69,6 @@ def process_image(image, shouldConvert=False):
70
 
71
  return filename
72
 
73
-
74
-
75
- def generate(image, query):
76
- imageFile = process_image(image)
77
- content = [
78
- {'image': f'file://{imageFile}'},
79
- {'text': query}
80
- ]
81
- messages = [
82
- {'role': 'user', 'content': content},
83
- ]
84
- print('messages:', messages)
85
- responses = MultiModalConversation.call(
86
- model='qvq-72b-preview', messages=messages, stream=True,
87
- )
88
- for response in responses:
89
- if not response.status_code == HTTPStatus.OK:
90
- raise HTTPError(f'response.code: {response.code}\nresponse.message: {response.message}')
91
- response = response.output.choices[0].message.content
92
- if len(response) > 0 and response[0]['text']:
93
- print(response[0]['text'])
94
- yield response[0]['text']
95
-
96
-
97
  if __name__ == "__main__":
98
 
99
  def on_clear():
@@ -196,6 +171,9 @@ if __name__ == "__main__":
196
  with antd.Card(title=get_text("Answer", "答案"),
197
  elem_style=dict(height="100%"),
198
  elem_id="output-container"):
 
 
 
199
  output = gr.Markdown(
200
  show_copy_button=True,
201
  latex_delimiters=[{
@@ -239,12 +217,42 @@ if __name__ == "__main__":
239
  fn=lambda: gr.update(props=dict(open=False)),
240
  outputs=[tour])
241
 
242
- submit_btn.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  fn=generate,
244
  inputs=[*input_image, input],
245
- outputs=[output])
246
  clear_btn.click(
247
  fn=on_clear,
248
  outputs=[*input_image, input])
 
249
 
250
  demo.queue(default_concurrency_limit=50).launch()
 
12
 
13
  from pathlib import Path
14
 
15
+ os.environ['DASHSCOPE_HTTP_BASE_URL'] = 'https://dashscope.aliyuncs.com/api/v1'
16
+ # os.environ['DASHSCOPE_WEBSOCKET_BASE_URL'] = 'https://poc-dashscope.aliyuncs.com/api-ws/v1/inference'
17
 
18
  import dashscope
19
  from dashscope import MultiModalConversation
20
  API_KEY = os.environ['API_KEY']
 
21
  dashscope.api_key = API_KEY
 
22
 
23
  is_modelscope_studio = os.getenv('MODELSCOPE_ENVIRONMENT') == 'studio'
24
 
 
69
 
70
  return filename
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  if __name__ == "__main__":
73
 
74
  def on_clear():
 
171
  with antd.Card(title=get_text("Answer", "答案"),
172
  elem_style=dict(height="100%"),
173
  elem_id="output-container"):
174
+ with ms.Slot("extra"):
175
+ cancel_btn = antd.Button(get_text("Stop", "停止"),
176
+ block=True, disabled=True)
177
  output = gr.Markdown(
178
  show_copy_button=True,
179
  latex_delimiters=[{
 
217
  fn=lambda: gr.update(props=dict(open=False)),
218
  outputs=[tour])
219
 
220
+ def generate(image, query):
221
+ imageFile = process_image(image)
222
+ content = [
223
+ {'image': f'file://{imageFile}'},
224
+ {'text': query}
225
+ ]
226
+ messages = [
227
+ {'role': 'user', 'content': content},
228
+ ]
229
+ print('messages:', messages)
230
+ responses = MultiModalConversation.call(
231
+ model='qvq-72b-preview', messages=messages, stream=True,
232
+ )
233
+ yield {
234
+ cancel_btn: gr.update(disabled=False)
235
+ }
236
+ for response in responses:
237
+ if not response.status_code == HTTPStatus.OK:
238
+ raise HTTPError(f'response.code: {response.code}\nresponse.message: {response.message}')
239
+ response = response.output.choices[0].message.content
240
+ if len(response) > 0 and response[0]['text']:
241
+ print(response[0]['text'])
242
+ yield {
243
+ output: response[0]['text']
244
+ }
245
+ yield {
246
+ cancel_btn: gr.update(disabled=True)
247
+ }
248
+
249
+ output_process = submit_btn.click(
250
  fn=generate,
251
  inputs=[*input_image, input],
252
+ outputs=[output, cancel_btn])
253
  clear_btn.click(
254
  fn=on_clear,
255
  outputs=[*input_image, input])
256
+ cancel_btn.click(fn=lambda : gr.update(disabled=True), inputs=None, outputs=[cancel_btn], cancels=[output_process])
257
 
258
  demo.queue(default_concurrency_limit=50).launch()