RajuKandasamy commited on
Commit
fa31131
1 Parent(s): 55f74bb

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +69 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
2
+ from threading import Thread
3
+ import gradio as gr
4
+
5
+ class ChatbotService:
6
+ def __init__(self, model_name="RajuKandasamy/marabutamil"):
7
+ self.model = AutoModelForCausalLM.from_pretrained(model_name)
8
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ self.streamer = None
10
+
11
+ def call(self, prompt):
12
+ self.streamer = TextIteratorStreamer(self.tokenizer, skip_prompt=True, timeout=5)
13
+ prompt = prompt.replace("<br>", "\n")
14
+ print(prompt)
15
+ inputs = self.tokenizer(prompt, return_tensors="pt")
16
+ print(inputs)
17
+ kwargs = dict(input_ids=inputs["input_ids"], streamer=self.streamer, max_new_tokens=256, do_sample=True, top_p=0.8, top_k=500, temperature=0.001, repetition_penalty=1.4)
18
+ thread = Thread(target=self.model.generate, kwargs=kwargs)
19
+ thread.start()
20
+ return ""
21
+
22
+
23
+ import gradio as gr
24
+
25
+ example_questions = [
26
+ f"""இன்னாமை வேண்டின்""",
27
+ f"""பாடல்:
28
+ நின்றன நின்றன நில்லாகும்""",
29
+ f"""பாடல்:
30
+ துகள்தீர் பெருஞ்செல்வம்""",
31
+ f"""பாடல்:
32
+ கொங்குதேர் வாழ்க்கை அஞ்சிறைத் தும்பி""",
33
+ f"""வேதம் உரைத்தானும் வேதிய னாகிலன்"""
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="Venba first Stanza:")
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()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ gradio
3
+ transformers==4.37.1