Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
model_name = "Jasoorhakimi/milad-farsi-gpt2"
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
def chat(input_text, history=[]):
|
11 |
+
inputs = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
|
12 |
+
outputs = model.generate(inputs, max_length=100, pad_token_id=tokenizer.eos_token_id)
|
13 |
+
reply = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
14 |
+
return reply
|
15 |
+
|
16 |
+
iface = gr.Interface(fn=chat,
|
17 |
+
inputs=gr.Textbox(lines=2, placeholder="پیام خود را وارد کنید..."),
|
18 |
+
outputs="text",
|
19 |
+
title="چتبات فارسی با GPT-2")
|
20 |
+
|
21 |
+
iface.launch()
|