GIGAParviz commited on
Commit
4fd7fd0
·
verified ·
1 Parent(s): 178a19f

Upload app.py

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