Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, jsonify, request
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
5 |
+
|
6 |
+
def format_prompt(message, history):
|
7 |
+
prompt = "<s>"
|
8 |
+
for user_prompt, bot_response in history:
|
9 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
10 |
+
prompt += f" {bot_response}</s> "
|
11 |
+
prompt += f"[INST] {message} [/INST]"
|
12 |
+
return prompt
|
13 |
+
|
14 |
+
def generate(
|
15 |
+
prompt, history, temperature=0.2, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.0,
|
16 |
+
):
|
17 |
+
temperature = float(temperature)
|
18 |
+
if temperature < 1e-2:
|
19 |
+
temperature = 1e-2
|
20 |
+
top_p = float(top_p)
|
21 |
+
|
22 |
+
generate_kwargs = dict(
|
23 |
+
temperature=temperature,
|
24 |
+
max_new_tokens=max_new_tokens,
|
25 |
+
top_p=top_p,
|
26 |
+
repetition_penalty=repetition_penalty,
|
27 |
+
do_sample=True,
|
28 |
+
seed=42,
|
29 |
+
)
|
30 |
+
|
31 |
+
formatted_prompt = format_prompt(prompt, history)
|
32 |
+
|
33 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
34 |
+
output = ""
|
35 |
+
|
36 |
+
for response in stream:
|
37 |
+
yield response.token.text
|
38 |
+
|
39 |
+
app = Flask(__name__)
|
40 |
+
|
41 |
+
@app.route('/health', methods=['GET'])
|
42 |
+
def health():
|
43 |
+
return jsonify({"status": "ok"})
|
44 |
+
|
45 |
+
@app.route('/completion', methods=['POST'])
|
46 |
+
def search_route():
|
47 |
+
data = request.get_json()
|
48 |
+
prompt = data.get('prompt', '')
|
49 |
+
|
50 |
+
|
51 |
+
if __name__ == '__main__':
|
52 |
+
app.run(debug=False, host='0.0.0.0', port=7860, threaded=False)
|