Commit
·
6f7eef0
1
Parent(s):
05c18a4
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import TextIteratorStreamer
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
from threading import Thread
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
class ChatbotService:
|
7 |
+
def __init__(self, model_name="RajuKandasamy/tamillama_tiny_30m"):
|
8 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
self.streamer = None
|
11 |
+
|
12 |
+
def call(self, prompt):
|
13 |
+
self.streamer = TextIteratorStreamer(self.tokenizer, skip_prompt=True, timeout=5)
|
14 |
+
prompt = prompt.replace("<br>", "\n")
|
15 |
+
print(prompt)
|
16 |
+
inputs = self.tokenizer(prompt, return_tensors="pt")
|
17 |
+
print(inputs)
|
18 |
+
kwargs = dict(input_ids=inputs["input_ids"], streamer=self.streamer, max_new_tokens=512, do_sample=True, top_p=0.8, top_k=500, temperature=0.001, repetition_penalty=1.4)
|
19 |
+
thread = Thread(target=self.model.generate, kwargs=kwargs)
|
20 |
+
thread.start()
|
21 |
+
return ""
|
22 |
+
|
23 |
+
import gradio as gr
|
24 |
+
|
25 |
+
example_questions = [
|
26 |
+
f"""கதை:
|
27 |
+
ஒரு காலத்தில் ஒரு பெரிய கோட்டையில் வாழ்ந்த அன்பான மனிதர் ஒருவர் இருந்தார்.""",
|
28 |
+
f"""சுருக்கம்:
|
29 |
+
இளவரசனும் அரக்கனும்""",
|
30 |
+
f"""Words: prevent, car, broken
|
31 |
+
Features: Dialogue""",
|
32 |
+
f"""சொற்கள்:
|
33 |
+
தீர்வு"""
|
34 |
+
]
|
35 |
+
|
36 |
+
|
37 |
+
chatbot_service = ChatbotService()
|
38 |
+
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
chatbot = gr.Chatbot().style(height=400)
|
42 |
+
with gr.Row():
|
43 |
+
msg = gr.Textbox(placeholder="Type your message here...", inputs="text",outputs="text", label="Story Prompt:")
|
44 |
+
run = gr.Button("Run")
|
45 |
+
examples_dropdown = gr.Dropdown(choices=example_questions, label="Select an example prompt")
|
46 |
+
examples_dropdown.change(fn=lambda x: x, inputs=examples_dropdown, outputs=msg)
|
47 |
+
|
48 |
+
clear = gr.Button("Clear")
|
49 |
+
|
50 |
+
def user(question, user_message, history):
|
51 |
+
if history == None:
|
52 |
+
history = []
|
53 |
+
user_message = question
|
54 |
+
return "", history + [[user_message, None]]
|
55 |
+
|
56 |
+
def bot(history):
|
57 |
+
#print("Question: ", history[-1][0])
|
58 |
+
chatbot_service.call(history[-1][0])
|
59 |
+
history[-1][1] = ""
|
60 |
+
for character in chatbot_service.streamer:
|
61 |
+
print(character)
|
62 |
+
history[-1][1] += character
|
63 |
+
yield history
|
64 |
+
|
65 |
+
run.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
|
66 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
67 |
+
|
68 |
+
demo.queue()
|
69 |
+
demo.launch()
|