Mikhail Nikolaev
commited on
Commit
Β·
c14a211
1
Parent(s):
1d27d36
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_id = "t-tech/T-pro-it-1.0"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
torch_dtype="auto",
|
| 10 |
+
device_map="auto"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def generate_response(prompt):
|
| 14 |
+
messages = [
|
| 15 |
+
{"role": "system", "content": "Π’Ρ T-pro, Π²ΠΈΡΡΡΠ°Π»ΡΠ½ΡΠΉ Π°ΡΡΠΈΡΡΠ΅Π½Ρ Π² Π’-Π’Π΅Ρ
Π½ΠΎΠ»ΠΎΠ³ΠΈΠΈ. Π’Π²ΠΎΡ Π·Π°Π΄Π°ΡΠ° - Π±ΡΡΡ ΠΏΠΎΠ»Π΅Π·Π½ΡΠΌ Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΡΠΌ Π°ΡΡΠΈΡΡΠ΅Π½ΡΠΎΠΌ."},
|
| 16 |
+
{"role": "user", "content": prompt}
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
text = tokenizer.apply_chat_template(
|
| 20 |
+
messages,
|
| 21 |
+
tokenize=False,
|
| 22 |
+
add_generation_prompt=True
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 26 |
+
generated_ids = model.generate(
|
| 27 |
+
**model_inputs,
|
| 28 |
+
max_new_tokens=256
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
generated_ids = [
|
| 32 |
+
output_ids[len(input_ids):]
|
| 33 |
+
for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 37 |
+
return response
|
| 38 |
+
|
| 39 |
+
interface = gr.Interface(
|
| 40 |
+
fn=generate_response,
|
| 41 |
+
inputs="text",
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="T-pro API"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
interface.launch()
|