Spaces:
Running
Running
HannahLin271
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from utils import init_model_from, respond
|
3 |
+
import tiktoken
|
4 |
+
# Configuration
|
5 |
+
max_new_tokens = 100
|
6 |
+
temperature = 0.5
|
7 |
+
top_k = 10
|
8 |
+
|
9 |
+
# Model information and links
|
10 |
+
|
11 |
+
model_info = {
|
12 |
+
"single_conversation_withGPTdata_bs256": {
|
13 |
+
'url': 'https://huggingface.co/HannahLin271/NanoGPT/resolve/main/singleConversation_withGPTdata_bs256.pt',
|
14 |
+
'description': "Trained on Facebook Emotion Dialogues dataset with additional GPT data, using a batch size of 256.",
|
15 |
+
'logo': '🧃'
|
16 |
+
},
|
17 |
+
"single_conversation_withGPTdata_withoutemotion": {
|
18 |
+
'url': 'https://huggingface.co/HannahLin271/NanoGPT/resolve/main/singleConversation_withGPTdata_withoutemotion.pt',
|
19 |
+
'description': "Trained on Facebook Emotion Dialogues dataset with GPT data, excluding emotion tag.",
|
20 |
+
'logo': '🧉'
|
21 |
+
},
|
22 |
+
"single_conversation_withcontext": {
|
23 |
+
'url': 'https://huggingface.co/HannahLin271/NanoGPT/resolve/main/singleConversation_withcontext.pt',
|
24 |
+
'description': "Trained on Facebook Emotion Dialogues dataset with context included for improved conversational understanding.",
|
25 |
+
'logo': '🍹'
|
26 |
+
},
|
27 |
+
"single_conversation_withemotion": {
|
28 |
+
'url': 'https://huggingface.co/HannahLin271/NanoGPT/resolve/main/singleConversation_withemotion.pt',
|
29 |
+
'description': "Trained on Facebook Emotion Dialogues dataset, retaining emotion annotations for each conversation.",
|
30 |
+
'logo': '🍺'
|
31 |
+
},
|
32 |
+
"single_conversation_withoutemotion": {
|
33 |
+
'url': 'https://huggingface.co/HannahLin271/NanoGPT/resolve/main/singleConversation_withoutemotion.pt',
|
34 |
+
'description': "Trained on Facebook Emotion Dialogues dataset, excluding emotion annotations for simpler conversations.",
|
35 |
+
'logo': '🍷'
|
36 |
+
},
|
37 |
+
"whole_conversation_withoutemotion": {
|
38 |
+
'url': 'https://huggingface.co/HannahLin271/NanoGPT/resolve/main/wholeConversation_withoutemotion.pt',
|
39 |
+
'description': "Trained on entire conversations from the Facebook Emotion Dialogues dataset, excluding tags other than <bot> and <human>.",
|
40 |
+
'logo': '🍵'
|
41 |
+
}
|
42 |
+
}
|
43 |
+
model_list = { }
|
44 |
+
model_choices = list(model_info.keys())
|
45 |
+
# init model for default selection
|
46 |
+
selected_model_name = "single_conversation_withGPTdata_bs256"
|
47 |
+
url = model_info[selected_model_name]['url']
|
48 |
+
model_list[selected_model_name] = init_model_from(url, selected_model_name)
|
49 |
+
|
50 |
+
# gpt-2 encodings
|
51 |
+
print("loading GPT-2 encodings...")
|
52 |
+
enc = tiktoken.get_encoding("gpt2")
|
53 |
+
encode = lambda s: enc.encode(s, allowed_special={"<|endoftext|>"})
|
54 |
+
decode = lambda l: enc.decode(l)
|
55 |
+
|
56 |
+
|
57 |
+
def predict(input: str, history: list = None) -> tuple:
|
58 |
+
if history is None:
|
59 |
+
history = [] # Initialize history if not provided
|
60 |
+
# Generate a response using the respond function
|
61 |
+
print(f"selected_model_name: {selected_model_name}")
|
62 |
+
response_data = respond(
|
63 |
+
input=input,
|
64 |
+
samples=1,
|
65 |
+
model=model_list[selected_model_name],
|
66 |
+
encode=encode,
|
67 |
+
decode=decode,
|
68 |
+
max_new_tokens=max_new_tokens,
|
69 |
+
temperature=temperature,
|
70 |
+
top_k=top_k,
|
71 |
+
)
|
72 |
+
|
73 |
+
response = response_data[1] # Extract bot's response
|
74 |
+
history.append((input, response)) # Append the user input and bot response to history
|
75 |
+
|
76 |
+
return history, history # Return updated history twice (for chatbot and state)
|
77 |
+
|
78 |
+
def prepare_model(selected_model):
|
79 |
+
global selected_model_name
|
80 |
+
selected_model_name = selected_model
|
81 |
+
url = model_info[selected_model]['url']
|
82 |
+
if selected_model not in model_list:
|
83 |
+
model_list[selected_model] = init_model_from(url, selected_model)
|
84 |
+
logo = model_info[selected_model]['logo']
|
85 |
+
description = model_info[selected_model]['description']
|
86 |
+
return f"## {logo}Model Information\n<br>Model_name: {selected_model}\n<br>Description: {description}"
|
87 |
+
|
88 |
+
default_model_info = f"## 🍭Model Information\n<br>Model_name: Name of the model\n<br>Description: How we train the model"
|
89 |
+
app = gr.Blocks()
|
90 |
+
|
91 |
+
with app:
|
92 |
+
gr.Markdown("# 🫂 Chatbot for ML Project\n### 🤗 Chat with your chatbot!")
|
93 |
+
# Model Parameters interface
|
94 |
+
inp = gr.Dropdown(
|
95 |
+
choices=model_choices,
|
96 |
+
label="Select a Model",
|
97 |
+
info="Choose a pre-trained model to power the chatbot."
|
98 |
+
)
|
99 |
+
out = gr.Markdown(value=default_model_info)
|
100 |
+
inp.change(prepare_model, inp, out)
|
101 |
+
|
102 |
+
# Chatbot interface
|
103 |
+
chat_interface = gr.Interface(
|
104 |
+
fn=predict,
|
105 |
+
inputs=[
|
106 |
+
gr.Textbox(lines=2, placeholder="Enter your message here...", label="User Input"),
|
107 |
+
gr.State(), # Maintain conversation state
|
108 |
+
],
|
109 |
+
outputs=[
|
110 |
+
gr.Chatbot(label="Chatbot Response"), # Display responses in chat format
|
111 |
+
gr.State() # Return the updated state
|
112 |
+
],
|
113 |
+
description="Your AI-based chatbot powered by selected models!"
|
114 |
+
)
|
115 |
+
#TODO: add emotion/context here
|
116 |
+
if __name__ == "__main__":
|
117 |
+
app.launch(share=True)
|