GIGAParviz commited on
Commit
b228807
·
verified ·
1 Parent(s): a763bd5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GenerationConfig
4
+ import re
5
+ import time
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained("GIGAParviz/T5_fa_law_chatbot")
8
+ model = AutoModelForSeq2SeqLM.from_pretrained("GIGAParviz/T5_fa_law_chatbot").to("cpu")
9
+
10
+ def clear_text(text):
11
+ if isinstance(text, str):
12
+ text = re.sub("[^آ-ی۰-۹]+", " ", text)
13
+ else:
14
+ text = str(text)
15
+ return text
16
+
17
+ def generate_response(message, chat_history):
18
+ question = clear_text(message)
19
+ prompt = f"Answer The Question in farsi: {question} Answer: "
20
+
21
+ tokenized_test_text = tokenizer(prompt, return_tensors='pt').input_ids.to("cpu")
22
+
23
+ generation_config = GenerationConfig(
24
+ max_new_tokens=128,
25
+ do_sample=True,
26
+ top_k=50,
27
+ top_p=0.95,
28
+ temperature=0.7,
29
+ repetition_penalty=1.2
30
+ )
31
+
32
+ test_output = model.generate(tokenized_test_text, generation_config=generation_config)[0]
33
+ bot_message = tokenizer.decode(test_output, skip_special_tokens=True)
34
+
35
+ for i in range(0, len(bot_message), 10):
36
+ yield chat_history + [(message, bot_message[:i + 10])]
37
+ time.sleep(0.1)
38
+
39
+ yield chat_history + [(message, bot_message)]
40
+
41
+ with gr.Blocks() as demo:
42
+ gr.Markdown("<h1 style='text-align: center;'>💬Persian legal chatbot </h1><p style='text-align: center;'>made by A.M.Parviz/p>")
43
+
44
+ chatbot = gr.Chatbot(label="جواب")
45
+ msg = gr.Textbox(label="ورودی", placeholder="...سوال حقوقی خودتون رو وارد کنید", lines=1)
46
+
47
+ msg.submit(generate_response, [msg, chatbot], chatbot)
48
+
49
+ clear = gr.ClearButton([msg, chatbot])
50
+
51
+ demo.launch()