Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -158,6 +158,20 @@ def function(Textbox, Textbox2, Textbox3, Dropdown):
|
|
158 |
|
159 |
if Textbox3 == target:
|
160 |
if Dropdown == "Option1":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
162 |
Textbox2 = Textbox2.split(",")
|
163 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
@@ -174,17 +188,55 @@ def function(Textbox, Textbox2, Textbox3, Dropdown):
|
|
174 |
)
|
175 |
try:
|
176 |
sleep(0.8)
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
# response = requests.post(target2, json=data)
|
182 |
# print("target2",target2)
|
183 |
# reply = response.content.decode("utf-8")
|
184 |
# reply = reply.replace(" ", "%20")
|
185 |
# image_array = download_image(f"https://api.placid.app/u/pydav4ibo?quote[text]={reply}")
|
186 |
data["messages"].append({"role": "assistant", "content": reply})
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
except Exception as e:
|
189 |
print(traceback.format_exc())
|
190 |
return "Please Wait!"
|
|
|
158 |
|
159 |
if Textbox3 == target:
|
160 |
if Dropdown == "Option1":
|
161 |
+
def get_current_time(location):
|
162 |
+
url = "https://api.api-ninjas.com/v1/worldtime?city="
|
163 |
+
url += location
|
164 |
+
headers = {
|
165 |
+
'X-Api-Key': 'Fl79Q6SqQz8BWQcaYGeQ5A==o5DKruyij5IfnMLM'
|
166 |
+
}
|
167 |
+
response = requests.request("GET", url, headers=headers)
|
168 |
+
data = response.json()
|
169 |
+
if response.status_code == 200:
|
170 |
+
time = data
|
171 |
+
return f"The current time in {location} is {time}."
|
172 |
+
else:
|
173 |
+
return f"Failed to retrieve the current time for {location}."
|
174 |
+
|
175 |
Textbox2 = Textbox2.replace("[", "").replace("]", "").replace("'", "")
|
176 |
Textbox2 = Textbox2.split(",")
|
177 |
Textbox2_edited = [x.strip() for x in Textbox2]
|
|
|
188 |
)
|
189 |
try:
|
190 |
sleep(0.8)
|
191 |
+
def chat_with_assistant(messages, functions=None):
|
192 |
+
chat = openai.ChatCompletion.create(
|
193 |
+
model=model, messages=data["messages"]
|
194 |
+
)
|
195 |
+
reply = chat.choices[0].message.content
|
196 |
+
return reply
|
197 |
+
|
198 |
+
functions = [
|
199 |
+
{
|
200 |
+
"name": "get_current_time",
|
201 |
+
"description": "Get the current time in a given location",
|
202 |
+
"parameters": {
|
203 |
+
"type": "object",
|
204 |
+
"properties": {
|
205 |
+
"location": {
|
206 |
+
"type": "string",
|
207 |
+
"description": "The city and state, e.g. San Francisco, CA",
|
208 |
+
},
|
209 |
+
"unit": {"type": "string", "enum": ["time", "date", "both"]},
|
210 |
+
},
|
211 |
+
"required": ["location"],
|
212 |
+
},
|
213 |
+
}
|
214 |
+
]
|
215 |
# response = requests.post(target2, json=data)
|
216 |
# print("target2",target2)
|
217 |
# reply = response.content.decode("utf-8")
|
218 |
# reply = reply.replace(" ", "%20")
|
219 |
# image_array = download_image(f"https://api.placid.app/u/pydav4ibo?quote[text]={reply}")
|
220 |
data["messages"].append({"role": "assistant", "content": reply})
|
221 |
+
assistant_response = chat_with_assistant(messages, functions)
|
222 |
+
if assistant_response.get("function_call"):
|
223 |
+
function_name = assistant_response["function_call"]["name"]
|
224 |
+
function_args = json.loads(assistant_response["function_call"]["arguments"])
|
225 |
+
if function_name == "get_current_time":
|
226 |
+
time = get_current_time(function_args["location"])
|
227 |
+
result = time
|
228 |
+
else:
|
229 |
+
return {assistant_response['content']}
|
230 |
+
|
231 |
+
data["messages"].append({
|
232 |
+
"role": "function",
|
233 |
+
"name": function_name,
|
234 |
+
"content": str(result)
|
235 |
+
})
|
236 |
+
|
237 |
+
assistant_response = chat_with_assistant(messages, functions)
|
238 |
+
return {assistant_response['content']}
|
239 |
+
data["messages"].append(assistant_response)
|
240 |
except Exception as e:
|
241 |
print(traceback.format_exc())
|
242 |
return "Please Wait!"
|