Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,7 @@ def get_apartment_data(query):
|
|
| 19 |
rows = worksheet.get_all_records()
|
| 20 |
results = []
|
| 21 |
for row in rows:
|
|
|
|
| 22 |
if query.lower() in row["district"].lower() or query.lower() in row["developer"].lower():
|
| 23 |
results.append(row)
|
| 24 |
if results:
|
|
@@ -31,36 +32,48 @@ def get_apartment_data(query):
|
|
| 31 |
|
| 32 |
# Function to handle queries
|
| 33 |
def respond(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
print("Query received:", message)
|
| 35 |
print("Chat history:", history)
|
| 36 |
|
| 37 |
-
if not history:
|
| 38 |
-
history = []
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
-
# If the query is
|
| 42 |
if "district" in message.lower() or "developer" in message.lower():
|
| 43 |
response = get_apartment_data(message)
|
| 44 |
else:
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
generated_response = client.chat_completion(
|
| 48 |
-
messages=
|
| 49 |
max_tokens=512,
|
| 50 |
temperature=0.7,
|
| 51 |
top_p=0.95,
|
| 52 |
)
|
| 53 |
response = generated_response["choices"][0]["message"]["content"]
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
history.append({"role": "assistant", "content": response})
|
| 58 |
-
return response, history
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Error processing query: {e}")
|
| 62 |
-
|
| 63 |
-
return "An error occurred while processing your query.", history
|
| 64 |
|
| 65 |
# Gradio Interface
|
| 66 |
chat_interface = gr.ChatInterface(
|
|
|
|
| 19 |
rows = worksheet.get_all_records()
|
| 20 |
results = []
|
| 21 |
for row in rows:
|
| 22 |
+
# Basic check if the query is found in "district" or "developer"
|
| 23 |
if query.lower() in row["district"].lower() or query.lower() in row["developer"].lower():
|
| 24 |
results.append(row)
|
| 25 |
if results:
|
|
|
|
| 32 |
|
| 33 |
# Function to handle queries
|
| 34 |
def respond(message, history):
|
| 35 |
+
"""
|
| 36 |
+
message (str): latest user message
|
| 37 |
+
history (list): the entire conversation so far, as a list of dicts like:
|
| 38 |
+
[
|
| 39 |
+
{"role": "user", "content": "..."},
|
| 40 |
+
{"role": "assistant", "content": "..."},
|
| 41 |
+
...
|
| 42 |
+
]
|
| 43 |
+
Returns a single string (the assistant's answer).
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
print("Query received:", message)
|
| 47 |
print("Chat history:", history)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
# If the query is about district or developer, fetch from Sheets
|
| 51 |
if "district" in message.lower() or "developer" in message.lower():
|
| 52 |
response = get_apartment_data(message)
|
| 53 |
else:
|
| 54 |
+
# Build messages from history for context
|
| 55 |
+
conversation = []
|
| 56 |
+
for turn in history:
|
| 57 |
+
conversation.append({"role": turn["role"], "content": turn["content"]})
|
| 58 |
+
|
| 59 |
+
# Add the new user message
|
| 60 |
+
conversation.append({"role": "user", "content": message})
|
| 61 |
+
|
| 62 |
+
# Query Zephyr
|
| 63 |
generated_response = client.chat_completion(
|
| 64 |
+
messages=conversation,
|
| 65 |
max_tokens=512,
|
| 66 |
temperature=0.7,
|
| 67 |
top_p=0.95,
|
| 68 |
)
|
| 69 |
response = generated_response["choices"][0]["message"]["content"]
|
| 70 |
|
| 71 |
+
# Just return the assistant's text
|
| 72 |
+
return response
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error processing query: {e}")
|
| 76 |
+
return "An error occurred while processing your query."
|
|
|
|
| 77 |
|
| 78 |
# Gradio Interface
|
| 79 |
chat_interface = gr.ChatInterface(
|