Spaces:
Sleeping
Sleeping
init
Browse files- Dockerfile +22 -0
- app.py +101 -0
- requirements +3 -0
Dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.10
|
3 |
+
# Set Python to use unbuffered mode
|
4 |
+
ENV PYTHONUNBUFFERED 1
|
5 |
+
# Set the working directory in the container
|
6 |
+
RUN mkdir /var/www
|
7 |
+
ENV HOME /var/www
|
8 |
+
WORKDIR /var/www
|
9 |
+
|
10 |
+
# Copy the current directory contents into the container at /app
|
11 |
+
COPY . /var/www
|
12 |
+
|
13 |
+
RUN pip install -r requirements
|
14 |
+
|
15 |
+
# Make port 5000 available to the world outside this container
|
16 |
+
EXPOSE 7860
|
17 |
+
|
18 |
+
# Define environment variable
|
19 |
+
ENV FLASK_APP app.py
|
20 |
+
|
21 |
+
# Run app.py when the container launches
|
22 |
+
CMD flask run --host=0.0.0.0 --port=7860
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, Response, jsonify
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from flask_cors import CORS
|
4 |
+
import json
|
5 |
+
|
6 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
7 |
+
|
8 |
+
summary_prompt = '<s>[INST]Ты ассистент. Отвечаешь на русском языке. Сформируй краткое изложение следующего текста: {}[/INST]'
|
9 |
+
|
10 |
+
def format_prompt(message, history):
|
11 |
+
prompt = "<s>"
|
12 |
+
for user_prompt, bot_response in history:
|
13 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
14 |
+
prompt += f" {bot_response}</s> "
|
15 |
+
prompt += f"[INST] {message} [/INST]"
|
16 |
+
return prompt
|
17 |
+
|
18 |
+
def generate(
|
19 |
+
prompt, history=[], temperature=0, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.0,
|
20 |
+
):
|
21 |
+
temperature = float(temperature)
|
22 |
+
if temperature < 1e-2:
|
23 |
+
temperature = 1e-2
|
24 |
+
top_p = float(top_p)
|
25 |
+
|
26 |
+
generate_kwargs = dict(
|
27 |
+
temperature=temperature,
|
28 |
+
max_new_tokens=max_new_tokens,
|
29 |
+
top_p=top_p,
|
30 |
+
repetition_penalty=repetition_penalty,
|
31 |
+
do_sample=True,
|
32 |
+
seed=42,
|
33 |
+
)
|
34 |
+
|
35 |
+
#formatted_prompt = format_prompt(prompt, history)
|
36 |
+
|
37 |
+
#stream = client.text_generation(prompt, **generate_kwargs, stream=True, details=False, return_full_text=False)
|
38 |
+
response = client.text_generation(prompt, **generate_kwargs, stream=False, details=False, return_full_text=False)
|
39 |
+
print(response)
|
40 |
+
return response.encode('utf-8')
|
41 |
+
#output = ""
|
42 |
+
|
43 |
+
#for response in stream:
|
44 |
+
# yield response.token.text.encode('utf-8')
|
45 |
+
|
46 |
+
app = Flask(__name__)
|
47 |
+
CORS(app)
|
48 |
+
|
49 |
+
@app.route('/health', methods=['GET'])
|
50 |
+
def health():
|
51 |
+
return jsonify({"status": "ok"})
|
52 |
+
|
53 |
+
@app.route('/completion', methods=['POST'])
|
54 |
+
def completion_route():
|
55 |
+
data = request.get_json()
|
56 |
+
prompt = data.get('prompt', '')
|
57 |
+
#truncated_prompt = prompt[:32768]
|
58 |
+
return Response(generate(prompt[:52768]), content_type='text/plain; charset=utf-8', status=200, direct_passthrough=True)
|
59 |
+
|
60 |
+
@app.route('/getsummary', methods=['POST'])
|
61 |
+
def summary_route():
|
62 |
+
data = request.get_json()
|
63 |
+
text = data.get('text', '')
|
64 |
+
|
65 |
+
summary_prompt = f'''<s>[INST]Ты ассистент. Отвечаешь на русском языке. Сформируй краткое изложение следующего текста: {text}[/INST]'''
|
66 |
+
response = generate(summary_prompt[:52000])
|
67 |
+
|
68 |
+
return Response(json.dumps({'result': response}, ensure_ascii=False, sort_keys=False), content_type='text/json; charset=utf-8', status=200, direct_passthrough=False)
|
69 |
+
|
70 |
+
@app.route('/cleantext', methods=['POST'])
|
71 |
+
def summary_route():
|
72 |
+
data = request.get_json()
|
73 |
+
text = data.get('text', '')
|
74 |
+
|
75 |
+
summary_prompt = f'''<s>[INST]Ты ассистент. Отвечаешь на русском языке. Сформируй краткое изложение следующего текста: {text}[/INST]'''
|
76 |
+
response = generate(summary_prompt[:52000])
|
77 |
+
|
78 |
+
return Response(json.dumps({'result': response}, ensure_ascii=False, sort_keys=False), content_type='text/json; charset=utf-8', status=200, direct_passthrough=False)
|
79 |
+
|
80 |
+
@app.route('/getfollowup', methods=['POST'])
|
81 |
+
def summary_route():
|
82 |
+
data = request.get_json()
|
83 |
+
text = data.get('text', '')
|
84 |
+
|
85 |
+
summary_prompt = f'''<s>[INST]Ты ассистент. Отвечаешь на русском языке. Сформируй краткое изложение следующего текста: {text}[/INST]'''
|
86 |
+
response = generate(summary_prompt[:52000])
|
87 |
+
|
88 |
+
return Response(json.dumps({'result': response}, ensure_ascii=False, sort_keys=False), content_type='text/json; charset=utf-8', status=200, direct_passthrough=False)
|
89 |
+
|
90 |
+
@app.route('/getagenda', methods=['POST'])
|
91 |
+
def summary_route():
|
92 |
+
data = request.get_json()
|
93 |
+
text = data.get('text', '')
|
94 |
+
|
95 |
+
summary_prompt = f'''<s>[INST]Ты ассистент. Отвечаешь на русском языке. Сформируй краткое изложение следующего текста: {text}[/INST]'''
|
96 |
+
response = generate(summary_prompt[:52000])
|
97 |
+
|
98 |
+
return Response(json.dumps({'result': response}, ensure_ascii=False, sort_keys=False), content_type='text/json; charset=utf-8', status=200, direct_passthrough=False)
|
99 |
+
|
100 |
+
if __name__ == '__main__':
|
101 |
+
app.run(debug=False, host='0.0.0.0', port=7860)
|
requirements
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
flask-cors
|
3 |
+
huggingface_hub
|