Spaces:
Build error
Build error
Commit
·
5949885
1
Parent(s):
801d255
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
-
import torch
|
4 |
-
from transformers import AutoConfig, AutoTokenizer, AutoModelWithLMHead
|
5 |
-
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
6 |
-
from itertools import chain
|
7 |
-
|
8 |
import os
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
#emotion_tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion")
|
11 |
#emotion_model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-emotion")
|
12 |
|
|
|
13 |
def get_emotion(text):
|
14 |
input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
|
15 |
output = model.generate(input_ids=input_ids,max_length=2)
|
@@ -17,61 +20,8 @@ def get_emotion(text):
|
|
17 |
label = dec[0]
|
18 |
return label.split()[1]
|
19 |
|
20 |
-
|
21 |
-
config = AutoConfig.from_pretrained('gorkemgoknar/gpt2chatbotenglish')
|
22 |
-
model = GPT2LMHeadModel.from_pretrained('gorkemgoknar/gpt2chatbotenglish', config=config)
|
23 |
-
|
24 |
-
tokenizer = GPT2Tokenizer.from_pretrained('gorkemgoknar/gpt2chatbotenglish')
|
25 |
-
tokenizer.model_max_length = 1024
|
26 |
-
|
27 |
-
#Dynamic Temperature
|
28 |
-
#See experiment https://www.linkedin.com/pulse/ai-goes-job-interview-g%25C3%25B6rkem-g%25C3%25B6knar
|
29 |
-
|
30 |
-
base_temperature = 1.2
|
31 |
-
dynamic_temperature_range = 0.15
|
32 |
-
|
33 |
-
rand_range = random.uniform(-1 * dynamic_temperature_range , dynamic_temperature_range )
|
34 |
-
temperature = base_temperature + rand_range
|
35 |
-
|
36 |
-
SPECIAL_TOKENS = ["<bos>", "<eos>", "<speaker1>", "<speaker2>", "<pad>"]
|
37 |
-
|
38 |
#See document for experiment https://www.linkedin.com/pulse/ai-goes-job-interview-g%C3%B6rkem-g%C3%B6knar/
|
39 |
|
40 |
-
def get_chat_response(name,history=[], input_txt = "Hello , what is your name?"):
|
41 |
-
|
42 |
-
ai_history = history.copy()
|
43 |
-
|
44 |
-
#ai_history.append(input_txt)
|
45 |
-
ai_history_e = [tokenizer.encode(e) for e in ai_history]
|
46 |
-
|
47 |
-
personality = "My name is " + name
|
48 |
-
|
49 |
-
bos, eos, speaker1, speaker2 = tokenizer.convert_tokens_to_ids(SPECIAL_TOKENS[:-1])
|
50 |
-
|
51 |
-
#persona first, history next, input text must be at the end
|
52 |
-
#[[bos, persona] , [history] , [input]]
|
53 |
-
sequence = [[bos] + tokenizer.encode(personality)] + ai_history_e + [tokenizer.encode(input_txt)]
|
54 |
-
##[[bos, persona] , [speaker1 .., speakser2 .., speaker1 ... speaker2 ... , [input]]
|
55 |
-
sequence = [sequence[0]] + [[speaker2 if (len(sequence)-i) % 2 else speaker1] + s for i, s in enumerate(sequence[1:])]
|
56 |
-
|
57 |
-
sequence = list(chain(*sequence))
|
58 |
-
|
59 |
-
#bot_input_ids = tokenizer.encode(personality + tokenizer.eos_token + input_txt + tokenizer.eos_token , return_tensors='pt')
|
60 |
-
sequence_len = len(sequence)
|
61 |
-
|
62 |
-
#optimum response and speed
|
63 |
-
chat_history_ids = model.generate(
|
64 |
-
torch.tensor(sequence).unsqueeze(0), max_length=50,
|
65 |
-
pad_token_id=tokenizer.eos_token_id,
|
66 |
-
no_repeat_ngram_size=3,
|
67 |
-
do_sample=True,
|
68 |
-
top_k=60,
|
69 |
-
top_p=0.8,
|
70 |
-
temperature = 1.3
|
71 |
-
)
|
72 |
-
out_str = tokenizer.decode(chat_history_ids[0][sequence_len:], skip_special_tokens=True)
|
73 |
-
#out_str = tokenizer.decode(chat_history_ids[:, sequence.shape[-1]:][0], skip_special_tokens=False)
|
74 |
-
return out_str
|
75 |
|
76 |
##you can use anyone from below
|
77 |
'''
|
@@ -101,10 +51,7 @@ def get_chat_response(name,history=[], input_txt = "Hello , what is your name?")
|
|
101 |
| Brother | Allnut | Rose | Qui-Gon | Jar Jar
|
102 |
'''
|
103 |
|
104 |
-
MODEL_NAME= "tts_models/multilingual/multi-dataset/your_tts"
|
105 |
-
|
106 |
|
107 |
-
## SEE https://huggingface.co/spaces/gorkemgoknar/movie_chat_gpt_yourtts_fileinput for actual call
|
108 |
|
109 |
|
110 |
|
@@ -141,7 +88,7 @@ interface_file= gr.Interface.load("gorkemgoknar/movie_chat_gpt_yourtts_fileinput
|
|
141 |
"text",
|
142 |
"state"],
|
143 |
outputs=["html","state",gr.outputs.Audio(type="file")],
|
144 |
-
css=css, title=
|
145 |
|
146 |
|
147 |
interface_mic= gr.Interface.load("gorkemgoknar/movie_chat_gpt_yourtts",
|
@@ -151,7 +98,7 @@ interface_mic= gr.Interface.load("gorkemgoknar/movie_chat_gpt_yourtts",
|
|
151 |
"text",
|
152 |
"state"],
|
153 |
outputs=["html","state",gr.outputs.Audio(type="file")],
|
154 |
-
css=css, title=
|
155 |
|
156 |
|
157 |
interface_text = gr.Interface.load("gorkemgoknar/moviechatbot",
|
@@ -162,6 +109,6 @@ interface_text = gr.Interface.load("gorkemgoknar/moviechatbot",
|
|
162 |
outputs=["html","state"],
|
163 |
css=css, title="Chat Text Only", description=description,article=article)
|
164 |
|
165 |
-
appinterface = gr.TabbedInterface([interface_mic, interface_text], ["Chat with Record",
|
166 |
if __name__ == "__main__":
|
167 |
appinterface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
|
5 |
+
# THIS IS TABBED INTERFACE that calls other spaces
|
6 |
+
## SEE https://huggingface.co/spaces/gorkemgoknar/movie_chat_gpt_yourtts_fileinput for file input
|
7 |
+
## SEE https://huggingface.co/spaces/gorkemgoknar/movie_chat_gpt_yourtts for mic input
|
8 |
+
## SEE https://huggingface.co/spaces/gorkemgoknar/moviechatbot for text input
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
#emotion_tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-emotion")
|
13 |
#emotion_model = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-emotion")
|
14 |
|
15 |
+
# TODO voice based on emotion
|
16 |
def get_emotion(text):
|
17 |
input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
|
18 |
output = model.generate(input_ids=input_ids,max_length=2)
|
|
|
20 |
label = dec[0]
|
21 |
return label.split()[1]
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
#See document for experiment https://www.linkedin.com/pulse/ai-goes-job-interview-g%C3%B6rkem-g%C3%B6knar/
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
##you can use anyone from below
|
27 |
'''
|
|
|
51 |
| Brother | Allnut | Rose | Qui-Gon | Jar Jar
|
52 |
'''
|
53 |
|
|
|
|
|
54 |
|
|
|
55 |
|
56 |
|
57 |
|
|
|
88 |
"text",
|
89 |
"state"],
|
90 |
outputs=["html","state",gr.outputs.Audio(type="file")],
|
91 |
+
css=css, title="Chat with Uploaded file", description=description,article=article )
|
92 |
|
93 |
|
94 |
interface_mic= gr.Interface.load("gorkemgoknar/movie_chat_gpt_yourtts",
|
|
|
98 |
"text",
|
99 |
"state"],
|
100 |
outputs=["html","state",gr.outputs.Audio(type="file")],
|
101 |
+
css=css, title="Chat with Your Voice", description=description,article=article )
|
102 |
|
103 |
|
104 |
interface_text = gr.Interface.load("gorkemgoknar/moviechatbot",
|
|
|
109 |
outputs=["html","state"],
|
110 |
css=css, title="Chat Text Only", description=description,article=article)
|
111 |
|
112 |
+
appinterface = gr.TabbedInterface([interface_mic,interface_file, interface_text], ["Chat with Mic Record","Chat with Audio Upload" , "Chat Text only"])
|
113 |
if __name__ == "__main__":
|
114 |
appinterface.launch()
|