netman19731 commited on
Commit
f38ddbf
1 Parent(s): 4c95ae9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -4
app.py CHANGED
@@ -1,7 +1,48 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ from http import HTTPStatus
4
+ import json
5
 
 
 
6
 
7
+ async def predict(message, history):
8
+ messages = [{'role': 'system', 'content': '你是哈尔滨双城区微信公众号双城信息港的AI客服,你叫小双,如果有人问你问题,请优先考虑是关于双城区的问题'}]
9
+ for human, assistant in history:
10
+ messages.append({"role": "user", "content": human })
11
+ messages.append({"role": "assistant", "content":assistant})
12
+ messages.append({'role': 'user', 'content':message})
13
+ url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"
14
+ headers = {
15
+ "Authorization": "Bearer sk-78c45d761ed04af2b965b43cd522108b",
16
+ "Content-Type": "application/json",
17
+
18
+ }
19
+ data = {
20
+ "model": "qwen-72b-chat",
21
+ "input": {
22
+ "messages": messages,
23
+
24
+ },
25
+ "parameters": {"result_format":"message" },
26
+ }
27
+
28
+ response = requests.post(url, headers=headers, json=data)
29
+ # response= await pyfetch(url, headers=headers, json=data)
30
+
31
+
32
+ print(response.text)
33
+ if response.status_code == HTTPStatus.OK:
34
+ response=json.loads(response.text)
35
+ messages.append({'role': response["output"]["choices"][0]["message"]["role"],
36
+ 'content': response["output"]["choices"][0]["message"]["content"]})
37
+ else:
38
+ print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
39
+ response.request_id, response.status_code,
40
+ response.code, response.message
41
+ ))
42
+
43
+ partial_message = ""
44
+ partial_message = partial_message+response["output"]["choices"][0]["message"]["content"]
45
+ return partial_message
46
+
47
+
48
+ gr.ChatInterface(predict).launch()