-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (61 loc) · 2.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from transformers import GPTNeoForCausalLM, GPT2Tokenizer
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
import torch
device = "cpu"
model_name="EleutherAI/gpt-neo-1.3B"
model = GPTNeoForCausalLM.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
with open("prompt.txt", "r") as file:
prompt = file.read()
print("Initial condition", prompt)
def parse_text(text):
lines = text.split("\n")
user_lines = []
alice_lines = []
current_speaker = ""
for line in lines:
line = line.strip()
if line.startswith("user:"):
current_speaker = "user"
user_lines.append(line[5:].strip())
elif line.startswith("Alice:"):
current_speaker = "Alice"
alice_lines.append(line[7:].strip())
else:
if current_speaker == "user":
user_lines[-1] = user_lines[-1] + " " + line
elif current_speaker == "Alice":
alice_lines[-1] = alice_lines[-1] + " " + line
return " ".join(user_lines), " ".join(alice_lines)
def chat_base(input):
p = prompt + input
input_ids = tokenizer(p, return_tensors="pt").input_ids
gen_tokens = model.generate(
input_ids,
do_sample=True,
temperature= 0.7,
repetition_penalty= 0.7,
min_length= 30,
max_length= 200)
gen_text = tokenizer.batch_decode(gen_tokens)[0]
#removes the prompt from the output
result = gen_text[len(p):]
print(">", result)
user, alice = parse_text(result)
print(">>", alice)
return alice
def gradioInterface(message):
history = gr.get_state() or []
print(history)
response = chat_base(message)
history.append((message, response))
gr.set_state(history)
html = "<div class='chatbot'>"
for user_msg, resp_msg in history:
html += f"<div class='user_msg'>{user_msg}</div>"
html += f"<div class='resp_msg'>{resp_msg}</div>"
html += "</div>"
return response
iface = gr.Interface(chat_base, gr.components.Textbox(label="Chat with Alice"), "text", allow_flagging="auto")
iface.launch()