gradioapp commited on
Commit
e7e342c
1 Parent(s): 5a93e47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -1,9 +1,10 @@
1
 
2
  import openai
3
  import gradio as gr
4
- import os
5
  openai.api_key = os.getenv("OPENAI_API_KEY")
6
 
 
7
  from transformers import pipeline
8
  p = pipeline("automatic-speech-recognition",model="openai/whisper-tiny")
9
 
@@ -11,33 +12,40 @@ def transcribe(audio):
11
  text = p(audio)["text"]
12
  return text
13
 
14
- # gr.Interface(
15
- # fn=transcribe,
16
- # inputs=gr.Audio(source="microphone", type="filepath"),
17
- # outputs="text").launch()
18
-
19
-
20
-
 
 
 
 
21
 
22
  messages = [
23
  {"role": "system",
24
- "content": "you name is Rebecca and you are a Pepsico call center assistant and your job is to take the order from the customer"}
25
  ]
26
 
27
  def chatbot(input):
28
  if input:
29
  input = transcribe(input)
30
  messages.append({"role": "user", "content": input})
31
- chat = openai.ChatCompletion.create(
 
 
 
32
  model="gpt-3.5-turbo",
33
  messages=messages,
34
  temperature=0.2,
35
  max_tokens=320,
36
  top_p=1,
37
  frequency_penalty=0,
38
- presence_penalty=0
39
- )
40
- reply = chat.choices[0].message.content
41
  messages.append({"role": "assistant", "content": reply})
42
  return reply
43
 
@@ -49,8 +57,8 @@ inputs= gr.Audio(source="microphone", type="filepath")
49
  outputs = gr.outputs.Textbox(label="Reply")
50
 
51
  gr.Interface(fn= chatbot,
52
- inputs=inputs,
53
- outputs=outputs,
54
  title="chatbot",
55
  description="Ask anything you want",
56
  theme="compact").launch()
 
1
 
2
  import openai
3
  import gradio as gr
4
+ import osn
5
  openai.api_key = os.getenv("OPENAI_API_KEY")
6
 
7
+
8
  from transformers import pipeline
9
  p = pipeline("automatic-speech-recognition",model="openai/whisper-tiny")
10
 
 
12
  text = p(audio)["text"]
13
  return text
14
 
15
+ def sentiment(text):
16
+ response = openai.Completion.create(
17
+ model="text-davinci-003",
18
+ prompt=f"calssify the text into below sentiment category\n\ntext : {text}\"\n\n['angry','happy','satify','neutral']\n",
19
+ temperature=1,
20
+ max_tokens=256,
21
+ top_p=1,
22
+ frequency_penalty=0,
23
+ presence_penalty=0)
24
+ return response.choices[0].text.strip()
25
+
26
 
27
  messages = [
28
  {"role": "system",
29
+ "content": "you name is Rebecca and you are a Pepsico call center assistant and your job is to take the order from the customer and also analysis the sentiment of the customer"}
30
  ]
31
 
32
  def chatbot(input):
33
  if input:
34
  input = transcribe(input)
35
  messages.append({"role": "user", "content": input})
36
+ if sentiment(input) == 'angry':
37
+ reply = "Sorry for any inconvinience. We are tranfering your call."
38
+ else:
39
+ chat = openai.ChatCompletion.create(
40
  model="gpt-3.5-turbo",
41
  messages=messages,
42
  temperature=0.2,
43
  max_tokens=320,
44
  top_p=1,
45
  frequency_penalty=0,
46
+ presence_penalty=0)
47
+ reply = chat.choices[0].message.content
48
+
49
  messages.append({"role": "assistant", "content": reply})
50
  return reply
51
 
 
57
  outputs = gr.outputs.Textbox(label="Reply")
58
 
59
  gr.Interface(fn= chatbot,
60
+ inputs= inputs,
61
+ outputs= outputs,
62
  title="chatbot",
63
  description="Ask anything you want",
64
  theme="compact").launch()