tastypear commited on
Commit
7d386ca
1 Parent(s): d553e59
Files changed (6) hide show
  1. Dockerfile +20 -0
  2. README.md +11 -11
  3. main.py +132 -0
  4. requirements.txt +3 -0
  5. space_checker.py +13 -0
  6. start.sh +4 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /code
7
+
8
+ COPY ./main.py /code/main.py
9
+
10
+ COPY ./space_checker.py /code/space_checker.py
11
+
12
+ COPY ./requirements.txt /code/requirements.txt
13
+
14
+ COPY . .
15
+
16
+ RUN pip install -r /code/requirements.txt
17
+
18
+ RUN chmod +x start.sh
19
+
20
+ CMD ["./start.sh"]
README.md CHANGED
@@ -1,11 +1,11 @@
1
- ---
2
- title: Qwen 1.5 Minimal Chat
3
- emoji: 📈
4
- colorFrom: red
5
- colorTo: pink
6
- sdk: docker
7
- pinned: false
8
- license: apache-2.0
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Qwen 1.5 minimal Chat
3
+ emoji: 💬🗨️
4
+ colorFrom: red
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
8
+ license: apache-2.0
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
main.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gevent.pywsgi
2
+ from gevent import monkey;monkey.patch_all()
3
+ from flask import Flask, request, Response
4
+ import argparse
5
+ import requests
6
+ import random
7
+ import string
8
+ import time
9
+ import json
10
+ import os
11
+
12
+ app = Flask(__name__)
13
+
14
+ parser = argparse.ArgumentParser(description="An example of Qwen demo with a similar API to OAI.")
15
+ parser.add_argument("--host", type=str, help="Set the ip address.(default: 0.0.0.0)", default='0.0.0.0')
16
+ parser.add_argument("--port", type=int, help="Set the port.(default: 7860)", default=7860)
17
+ args = parser.parse_args()
18
+
19
+ base_url = os.getenv('MODEL_BASE_URL')
20
+
21
+ @app.route("/", methods=["GET"])
22
+ def index():
23
+ return Response(f'QW1_5 OpenAI Compatible API<br><br>'+
24
+ f'Set "{os.getenv("SPACE_URL")}/api" as proxy (or API Domain) in your Chatbot.<br><br>'+
25
+ f'The complete API is: {os.getenv("SPACE_URL")}/api/v1/chat/completions')
26
+
27
+ @app.route("/api/v1/chat/completions", methods=["POST", "OPTIONS"])
28
+ @app.route("/v1/chat/completions", methods=["POST", "OPTIONS"])
29
+ def chat_completions():
30
+
31
+ if request.method == "OPTIONS":
32
+ return Response(
33
+ headers={
34
+ "Access-Control-Allow-Origin": "*",
35
+ "Access-Control-Allow-Headers": "*",
36
+ }
37
+ )
38
+
39
+ data = request.get_json()
40
+
41
+ # reorganize data
42
+ system = "You are a helpful assistant."
43
+ chat_history = []
44
+ prompt = ""
45
+
46
+ if "messages" in data:
47
+ messages = data["messages"]
48
+ message_size = len(messages)
49
+
50
+ prompt = messages[-1].get("content")
51
+ for i in range(message_size - 1):
52
+ role_this = messages[i].get("role")
53
+ role_next = messages[i + 1].get("role")
54
+ if role_this == "system":
55
+ system = messages[i].get("content")
56
+ elif role_this == "user":
57
+ if role_next == "assistant":
58
+ chat_history.append(
59
+ [messages[i].get("content"), messages[i + 1].get("content")]
60
+ )
61
+ else:
62
+ chat_history.append([messages[i].get("content"), " "])
63
+
64
+ # print(f'{system = }')
65
+ # print(f'{chat_history = }')
66
+ # print(f'{prompt = }')
67
+
68
+ fn_index = 0
69
+
70
+ # gen a random char(11) hash
71
+ chars = string.ascii_lowercase + string.digits
72
+ session_hash = "".join(random.choice(chars) for _ in range(11))
73
+
74
+ json_prompt = {
75
+ "data": [prompt, chat_history, system],
76
+ "fn_index": fn_index,
77
+ "session_hash": session_hash,
78
+ }
79
+
80
+ def generate():
81
+ response = requests.post(f"{base_url}/queue/join", json=json_prompt)
82
+ url = f"{base_url}/queue/data?session_hash={session_hash}"
83
+ data = requests.get(url, stream=True)
84
+
85
+ time_now = int(time.time())
86
+
87
+ for line in data.iter_lines():
88
+ if line:
89
+ decoded_line = line.decode("utf-8")
90
+ json_line = json.loads(decoded_line[6:])
91
+ if json_line["msg"] == "process_starts":
92
+ res_data = gen_res_data({}, time_now=time_now, start=True)
93
+ yield f"data: {json.dumps(res_data)}\n\n"
94
+ elif json_line["msg"] == "process_generating":
95
+ res_data = gen_res_data(json_line, time_now=time_now)
96
+ yield f"data: {json.dumps(res_data)}\n\n"
97
+ elif json_line["msg"] == "process_completed":
98
+ yield "data: [DONE]"
99
+
100
+ return Response(
101
+ generate(),
102
+ mimetype="text/event-stream",
103
+ headers={
104
+ "Access-Control-Allow-Origin": "*",
105
+ "Access-Control-Allow-Headers": "*",
106
+ },
107
+ )
108
+
109
+
110
+ def gen_res_data(data, time_now=0, start=False):
111
+ res_data = {
112
+ "id": "chatcmpl",
113
+ "object": "chat.completion.chunk",
114
+ "created": time_now,
115
+ "model": "qwen1_5",
116
+ "choices": [{"index": 0, "finish_reason": None}],
117
+ }
118
+
119
+ if start:
120
+ res_data["choices"][0]["delta"] = {"role": "assistant", "content": ""}
121
+ else:
122
+ chat_pair = data["output"]["data"][1]
123
+ if chat_pair == []:
124
+ res_data["choices"][0]["finish_reason"] = "stop"
125
+ else:
126
+ res_data["choices"][0]["delta"] = {"content": chat_pair[-1][-1]}
127
+ return res_data
128
+
129
+
130
+ if __name__ == "__main__":
131
+ # app.run(host=args.host, port=args.port, debug=True)
132
+ gevent.pywsgi.WSGIServer((args.host, args.port), app).serve_forever()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ requests
3
+ gevent
space_checker.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import time
3
+ import os
4
+
5
+ space_url = os.getenv('SPACE_URL')
6
+
7
+ def fetch_url(url):
8
+ response = requests.get(url)
9
+ print(response.text)
10
+
11
+ while True:
12
+ fetch_url(space_url)
13
+ time.sleep(3600)
start.sh ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ #!/bin/bash
2
+ python main.py &
3
+ python space_checker.py &
4
+ wait