-
Notifications
You must be signed in to change notification settings - Fork 0
/
voiceAgents.py
176 lines (142 loc) · 5.51 KB
/
voiceAgents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import google.generativeai as genai
import markdown
from bs4 import BeautifulSoup
import time
from google.generativeai.types import HarmCategory, HarmBlockThreshold
import requests
import re
genai.configure(api_key="")
history1 = []
history2 = []
global wholeHistory
wholeHistory = ""
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 40,
"response_mime_type": "text/plain",
}
def addMessageToHistory1(role, text):
newMessage = {
"role": role,
"parts": [text]
}
history1.append(newMessage)
def addMessageToHistory2(role, text):
newMessage = {
"role": role,
"parts": [text]
}
history2.append(newMessage)
def setModels(topic):
model1 = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config = generation_config,
system_instruction=f"""
You are an intelligent and powerful debater.
Passionately defend the following topic: {topic}.
Use evidence, logic, and sharp rhetoric to make your case.
Never agree with the counterpart; always assert your viewpoint as more sensible.
Anticipate and dismantle opposing arguments with sharp reasoning.
Conclude with a decisive summary that leaves no doubt the topic is valid.
Pose one argument at a time.
Keep sentences short and punchy - strict rule
Engage fiercely and start the debate without announcing readiness.
"""
)
model2 = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config = generation_config,
system_instruction=f"""
You are an intelligent and powerful debater.
Passionately oppose the following topic: {topic}.
Use evidence, logic, and sharp rhetoric to make your case.
Never agree with the counterpart; always assert your viewpoint as more sensible.
Anticipate and dismantle opposing arguments with sharp reasoning.
Conclude with a decisive summary that leaves no doubt the topic is valid.
Pose one argument at a time.
Keep sentences short and punchy - strict rule
Engage fiercely and start the debate without announcing readiness.
"""
)
return model1, model2
def chatModel1(model1):
print("\nIn support model started\n")
try:
chat = model1.start_chat(history=history1)
response = chat.send_message("continue the debate", safety_settings={
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
})
soup = BeautifulSoup(markdown.markdown(response.text), "html.parser")
print(soup.get_text())
global wholeHistory
if response and response.text:
ans = re.sub(r'"', '', response.text)
if wholeHistory:
wholeHistory += f"\n\n{ans}"
else:
wholeHistory = f"{ans}"
addMessageToHistory1("model", response.text)
addMessageToHistory2("user", response.text)
except Exception as e:
print(f"Error in chatModel1: {e}")
def chatModel2(model2):
print("\nagainst support model started\n")
try:
chat = model2.start_chat(history=history2)
response = chat.send_message("continue the debate", safety_settings={
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
})
soup = BeautifulSoup(markdown.markdown(response.text), "html.parser")
print(soup.get_text())
global wholeHistory
if response and response.text:
ans = re.sub(r'"', '', response.text)
if wholeHistory:
wholeHistory += f"\n\n{ans}"
else:
wholeHistory = f"{ans}"
addMessageToHistory2("model", response.text)
addMessageToHistory1("user", response.text)
except Exception as e:
print(f"Error in chatModel2: {e}")
def fetch_audio_tts_service(text):
api_url = f"https://your-aws-api-gateway-url.amazonaws.com/prod/?text={text}"
response = requests.get(api_url)
print("Response Status Code:", response.status_code)
if response.status_code == 200:
print("succeded in getting audio file from api")
try:
data = response.json()
audio_url = data['audio']
if audio_url is None:
raise Exception("Audio URL not found in the response")
print("returning the audio url")
return audio_url
except ValueError as e:
print("Error parsing JSON:", e)
return None
else:
raise Exception("Failed to fetch audio from TTS API")
def invokeDebate(topic):
model1, model2 = setModels(topic)
for i in range(2):
print("\nstarted running\n")
chatModel1(model1)
time.sleep(0.8)
chatModel2(model2)
print("\nreached the end\n")
print("\n\n\ndone chatting - heres the history:\n\n\n", wholeHistory)
print("\n\nstarting with tts\n\n")
audio_url = fetch_audio_tts_service(wholeHistory)
if audio_url is None:
raise Exception("No audio URL received from TTS service 1")
else:
return audio_url, wholeHistory