Cristian commited on
Commit
4496ce9
1 Parent(s): 844e150

code updated

Browse files
Files changed (2) hide show
  1. app.py +44 -4
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,7 +1,47 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.chains import ConversationChain
4
+ from transformers import pipeline
5
 
6
+ model_name="nateraw/bert-base-uncased-emotion"
7
+ model = pipeline('text-classification', model_name, truncation=True)
8
 
9
+ from transformers import AutoTokenizer, AutoModelWithLMHead
10
+ model_name = "mrm8488/t5-base-finetuned-emotion"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model_t5 = AutoModelWithLMHead.from_pretrained(model_name)
13
+
14
+ def get_emotion(text):
15
+ input_ids = tokenizer.encode(text + '</s>', return_tensors='pt')
16
+ output = model_t5.generate(input_ids=input_ids, return_dict_in_generate=True, output_scores=True)
17
+ transition_scores = model_t5.compute_transition_scores(output.sequences, [s.softmax(dim=1) for s in output.scores], normalize_logits=False)
18
+ dec = [tokenizer.decode(ids) for ids in output.sequences]
19
+ score = transition_scores.min().item()
20
+ return f"{dec[0].replace('<pad>','').replace('</s>','').strip()} [{score}]"
21
+
22
+ chat = ChatOpenAI()
23
+ conversation = ConversationChain(llm=chat)
24
+ #Write a text example of someone angry
25
+ with gr.Blocks() as demo:
26
+ label_text = gr.Textbox(label="Sentiment Text", text="")
27
+ chatbot = gr.Chatbot(scale=2)
28
+ msg = gr.Textbox()
29
+ clear = gr.ClearButton([msg, chatbot])
30
+
31
+
32
+ def respond(message, chat_history):
33
+ bot_message = conversation.run(message)
34
+ chat_history.append((message, bot_message))
35
+
36
+ l = model(bot_message)[0]
37
+ label_value = f"{l['label']} [{l['score']}]"
38
+ label_value_t5 = get_emotion(bot_message)
39
+
40
+ return "", chat_history, f"Model1: {label_value_t5} - Model2: {label_value}"
41
+
42
+ msg.submit(respond, [msg, chatbot], [msg, chatbot, label_text])
43
+
44
+ demo.launch()
45
+
46
+
47
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ langchain
2
+ openai