gen
Browse files
app.py
CHANGED
|
@@ -12,7 +12,36 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 12 |
print(f"load tokenizer {model_name}")
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from flask import Flask
|
|
|
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
print(f"Flask app")
|
|
@@ -31,5 +60,13 @@ def about():
|
|
| 31 |
def contact():
|
| 32 |
return "<h1>Контакты</h1><p>Свяжитесь с нами: email@example.com</p>"
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
if __name__ == "__main__":
|
| 35 |
app.run(debug=False, host='0.0.0.0', port=7860)
|
|
|
|
| 12 |
print(f"load tokenizer {model_name}")
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
|
| 15 |
+
|
| 16 |
+
def text_gen(msg):
|
| 17 |
+
|
| 18 |
+
prompt = "Give me a short introduction to large language models."
|
| 19 |
+
messages = [
|
| 20 |
+
{"role": "system", "content": "You are Ring, an assistant created by inclusionAI"},
|
| 21 |
+
{"role": "user", "content": prompt}
|
| 22 |
+
]
|
| 23 |
+
text = tokenizer.apply_chat_template(
|
| 24 |
+
messages,
|
| 25 |
+
tokenize=False,
|
| 26 |
+
add_generation_prompt=True,
|
| 27 |
+
enable_thinking=True
|
| 28 |
+
)
|
| 29 |
+
model_inputs = tokenizer([text], return_tensors="pt", return_token_type_ids=False).to(model.device)
|
| 30 |
+
|
| 31 |
+
generated_ids = model.generate(
|
| 32 |
+
**model_inputs,
|
| 33 |
+
max_new_tokens=8192
|
| 34 |
+
)
|
| 35 |
+
generated_ids = [
|
| 36 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
|
| 43 |
from flask import Flask
|
| 44 |
+
from flask import request
|
| 45 |
|
| 46 |
app = Flask(__name__)
|
| 47 |
print(f"Flask app")
|
|
|
|
| 60 |
def contact():
|
| 61 |
return "<h1>Контакты</h1><p>Свяжитесь с нами: email@example.com</p>"
|
| 62 |
|
| 63 |
+
# Страница "Контакты"
|
| 64 |
+
@app.route("/gen", methods=['POST', 'GET'])
|
| 65 |
+
def gen_msg():
|
| 66 |
+
if request.form.get('msg'):
|
| 67 |
+
answer = text_gen(request.form['msg'])
|
| 68 |
+
|
| 69 |
+
return f"<h1>{answer}</p>"
|
| 70 |
+
|
| 71 |
if __name__ == "__main__":
|
| 72 |
app.run(debug=False, host='0.0.0.0', port=7860)
|