Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,12 +36,19 @@ def respond(message, history):
|
|
| 36 |
history = []
|
| 37 |
|
| 38 |
try:
|
|
|
|
| 39 |
if "district" in message.lower() or "developer" in message.lower():
|
| 40 |
response = get_apartment_data(message)
|
| 41 |
else:
|
| 42 |
-
messages = [{"role": "
|
| 43 |
-
for
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
generated_response = client.chat_completion(
|
| 46 |
messages=messages,
|
| 47 |
max_tokens=512,
|
|
@@ -50,17 +57,22 @@ def respond(message, history):
|
|
| 50 |
)
|
| 51 |
response = generated_response["choices"][0]["message"]["content"]
|
| 52 |
|
| 53 |
-
|
| 54 |
-
history.append(
|
| 55 |
return response, history
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Error processing query: {e}")
|
| 59 |
-
history.append({"role": "assistant", "content": "An error occurred while processing your query."})
|
| 60 |
return "An error occurred while processing your query.", history
|
| 61 |
|
|
|
|
| 62 |
chat_interface = gr.ChatInterface(
|
| 63 |
-
respond
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 36 |
history = []
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
# Determine if the query should access Google Sheets
|
| 40 |
if "district" in message.lower() or "developer" in message.lower():
|
| 41 |
response = get_apartment_data(message)
|
| 42 |
else:
|
| 43 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 44 |
+
for user_message, assistant_message in history:
|
| 45 |
+
if user_message:
|
| 46 |
+
messages.append({"role": "user", "content": user_message})
|
| 47 |
+
if assistant_message:
|
| 48 |
+
messages.append({"role": "assistant", "content": assistant_message})
|
| 49 |
+
messages.append({"role": "user", "content": message})
|
| 50 |
+
|
| 51 |
+
# Generate response from model
|
| 52 |
generated_response = client.chat_completion(
|
| 53 |
messages=messages,
|
| 54 |
max_tokens=512,
|
|
|
|
| 57 |
)
|
| 58 |
response = generated_response["choices"][0]["message"]["content"]
|
| 59 |
|
| 60 |
+
# Append the new interaction to the history
|
| 61 |
+
history.append((message, response))
|
| 62 |
return response, history
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Error processing query: {e}")
|
|
|
|
| 66 |
return "An error occurred while processing your query.", history
|
| 67 |
|
| 68 |
+
# Set up Gradio Chat Interface
|
| 69 |
chat_interface = gr.ChatInterface(
|
| 70 |
+
fn=respond,
|
| 71 |
+
examples=[
|
| 72 |
+
"What apartments are available in Bostandyk?",
|
| 73 |
+
"Tell me about BI Group.",
|
| 74 |
+
"What options do you have for developers?",
|
| 75 |
+
],
|
| 76 |
)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|