sayanbanerjee32
commited on
Commit
•
b511184
1
Parent(s):
79200aa
Upload folder using huggingface_hub
Browse files- app.py +54 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
+
import time
|
5 |
+
|
6 |
+
REPO_ID = "sayanbanerjee32/ms-phi2-qlora-oasst1"
|
7 |
+
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(REPO_ID)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
|
10 |
+
|
11 |
+
def generate_text(prompt, chat_history, num_new_tokens = 100):
|
12 |
+
# prompt = "<|prompter|>What is 2 + 2?<|endoftext|><|assistant|>" # change to your desired prompt
|
13 |
+
|
14 |
+
|
15 |
+
input_prompt = ''
|
16 |
+
if len(chat_history) > 0:
|
17 |
+
input_prompt += "<|prompter|>" + chat_history[-1][0] + "<|endoftext|><|assistant|>" + chat_history[-1][1] + "<|endoftext|>"
|
18 |
+
input_prompt += "<|prompter|>" + prompt + "<|endoftext|><|assistant|>"
|
19 |
+
# Count the number of tokens in the prompt
|
20 |
+
num_prompt_tokens = len(tokenizer(input_prompt)['input_ids'])
|
21 |
+
# Calculate the maximum length for the generation
|
22 |
+
max_length = num_prompt_tokens + num_new_tokens
|
23 |
+
gen = pipeline('text-generation', model=model,
|
24 |
+
tokenizer=tokenizer, max_length= max_length )
|
25 |
+
result = gen(prompt)
|
26 |
+
return result[0]['generated_text'].replace(prompt, '')
|
27 |
+
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
gr.HTML("<h1 align = 'center'> Chat </h1>")
|
30 |
+
gr.HTML("<h4 align = 'center'> ChatBot powered by Microsoft-Phi-2 finetuned on OpenAssistant dataset</h4>")
|
31 |
+
|
32 |
+
chatbot = gr.Chatbot()
|
33 |
+
msg = gr.Textbox()
|
34 |
+
gr.Examples(["What do you think about ChatGPT?",
|
35 |
+
"How would the Future of AI in 10 Years look?",
|
36 |
+
"Write a announcement tweet for medium.com readers about the new blogpost on 'Open Assistant is open source ChatGPT that you don\'t wanna miss out'",
|
37 |
+
"Please implement the Timsort algorithm on Lean 4 and explain your code",
|
38 |
+
"How do I build a PC?"],
|
39 |
+
inputs = msg)
|
40 |
+
clear = gr.ClearButton([msg, chatbot])
|
41 |
+
|
42 |
+
def respond(message, chat_history):
|
43 |
+
bot_message = generate_text(message, chat_history)
|
44 |
+
chat_history.append((message, bot_message))
|
45 |
+
time.sleep(2)
|
46 |
+
return "", chat_history
|
47 |
+
|
48 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
49 |
+
|
50 |
+
# # for collab
|
51 |
+
# demo.launch(debug=True)
|
52 |
+
|
53 |
+
if __name__ == '__main__':
|
54 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
accelerate
|
3 |
+
transformers
|