osanseviero commited on
Commit
320951c
1 Parent(s): 35c9479

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -34
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import torch
3
  import numpy as np
@@ -10,6 +12,9 @@ from efficientat.helpers.utils import NAME_TO_WIDTH, labels
10
  from torch import autocast
11
  from contextlib import nullcontext
12
 
 
 
 
13
  MODEL_NAME = "mn40_as"
14
 
15
  device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
@@ -44,41 +49,48 @@ def audio_tag(
44
  sorted_indexes = np.argsort(preds)[::-1]
45
  output = {}
46
  # Print audio tagging top probabilities
47
- for k in range(10):
48
- output[sorted_indexes[k]] = labels[sorted_indexes[k]]
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- return "\n".join(output.values())
51
-
52
- def formatted_message(audio_length, audio_class, userText):
53
- prefix = '''You are going to act as a magical tool that allows for humans to communicate with non-human entities like
54
- rocks, crackling fire, trees, animals, and the wind. In order to do this, we're going to provide you a data string which
55
- represents the audio input, the source of the audio, and the human's text input for the conversation.
56
- The goal is for you to embody the source of the audio, and use the length and variance in the signal data to produce
57
- plausible responses to the humans input. Remember to embody the the source data. When we start the conversation,
58
- you should generate a "personality profile" for the source and utilize that personality profile in your responses.
59
- Let's begin:'''
60
- suffix = f'''Source: {audio_class}
61
- Length of Audio in Seconds: {audio_length}
62
- Human Input: {userText}
63
- {audio_class} Response:'''
64
-
65
- template = prefix + suffix
66
-
67
- response = call_api(template)
68
-
69
- return response
70
-
71
-
72
- def call_api(message):
73
- """
74
- response = requests.get(f'{api}?q={message}')
75
- if response.status_code == 200:
76
-
77
- return str(response.text).split('\n', 2)[2]
78
- else:
79
- return Sorry, I'm quite busy right now, but please try again later :)
80
- """
81
- return message
82
 
83
 
84
  demo = gr.Interface(
 
1
+ import os
2
+
3
  import gradio as gr
4
  import torch
5
  import numpy as np
 
12
  from torch import autocast
13
  from contextlib import nullcontext
14
 
15
+ from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
16
+ from langchain.chains.conversation.memory import ConversationalBufferWindowMemory
17
+
18
  MODEL_NAME = "mn40_as"
19
 
20
  device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
 
49
  sorted_indexes = np.argsort(preds)[::-1]
50
  output = {}
51
  # Print audio tagging top probabilities
52
+
53
+ label = labels[sorted_indexes[0]]
54
+ return formatted_message(label)
55
+
56
+
57
+ cached_audio_class = None
58
+ template = None
59
+ prompt = None
60
+ chain = None
61
+
62
+ def formatted_message(audio_class):
63
+ if cached_audio_class != audio_class:
64
+ cached_audio_class = audio_class
65
 
66
+ prefix = '''You are going to act as a magical tool that allows for humans to communicate with non-human entities like
67
+ rocks, crackling fire, trees, animals, and the wind. In order to do this, we're going to provide you a data string which
68
+ represents the audio input, the source of the audio, and the human's text input for the conversation.
69
+ The goal is for you to embody the source of the audio, and use the length and variance in the signal data to produce
70
+ plausible responses to the humans input. Remember to embody the the source data. When we start the conversation,
71
+ you should generate a "personality profile" for the source and utilize that personality profile in your responses.
72
+ Let's begin:'''
73
+ suffix = f'''Source: {audio_class}
74
+ Length of Audio in Seconds: {audio_length}
75
+ Human Input: {userText}
76
+ {audio_class} Response:'''
77
+ template = prefix + suffix
78
+
79
+ prompt = PromptTemplate(
80
+ input_variables=["history", "human_input"],
81
+ template=template
82
+ )
83
+
84
+ chatgpt_chain = LLMChain(
85
+ llm=OpenAI(temperature=.5, openai_api_key=session_token),
86
+ prompt=prompt,
87
+ verbose=True,
88
+ memory=ConversationalBufferWindowMemory(k=2),
89
+ )
90
+
91
+ output = chatgpt_chain.predict(human_input=message)
92
+
93
+ return output
 
 
 
 
94
 
95
 
96
  demo = gr.Interface(