Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,76 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
# Check
|
| 11 |
-
if "
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
history.append({"role": "user", "content": message})
|
| 32 |
history.append({"role": "assistant", "content": response})
|
| 33 |
return history
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import gspread
|
| 3 |
+
from google.oauth2.service_account import Credentials
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
|
| 6 |
+
# Google Sheets Configuration
|
| 7 |
+
SERVICE_ACCOUNT_FILE = "sheet-integration-agent@bold-physics-445007-g4.iam.gserviceaccount.com.json"
|
| 8 |
+
SPREADSHEET_ID = "1LHgb4eYDM2ImFY3e3h4kPUnDh6oRHpVz2PA29lQ3Xxg"
|
| 9 |
+
|
| 10 |
+
# Authenticate and connect to Google Sheets
|
| 11 |
+
scopes = ["https://www.googleapis.com/auth/spreadsheets"]
|
| 12 |
+
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=scopes)
|
| 13 |
+
client = gspread.authorize(credentials)
|
| 14 |
+
worksheet = client.open_by_key(SPREADSHEET_ID).sheet1
|
| 15 |
|
| 16 |
+
# Hugging Face Model Configuration
|
| 17 |
+
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
| 18 |
+
hf_client = InferenceClient(model_name)
|
| 19 |
|
| 20 |
+
def get_apartment_data(query):
|
| 21 |
+
"""
|
| 22 |
+
Fetch apartment data based on the user query from Google Sheets.
|
| 23 |
+
"""
|
| 24 |
+
rows = worksheet.get_all_records()
|
| 25 |
+
results = []
|
| 26 |
+
for row in rows:
|
| 27 |
+
if query.lower() in row.get("district", "").lower() or query.lower() in row.get("developer", "").lower():
|
| 28 |
+
results.append(row)
|
| 29 |
+
if results:
|
| 30 |
+
return "\n".join([str(result) for result in results])
|
| 31 |
+
return "No matching apartments found for your query."
|
| 32 |
+
|
| 33 |
+
def respond(message, history):
|
| 34 |
+
"""
|
| 35 |
+
Respond to user messages using Hugging Face and Google Sheets data.
|
| 36 |
+
"""
|
| 37 |
try:
|
| 38 |
+
# Check if the query relates to apartments
|
| 39 |
+
if "apartment" in message.lower() or "district" in message.lower():
|
| 40 |
+
sheet_response = get_apartment_data(message)
|
| 41 |
+
history.append({"role": "assistant", "content": sheet_response})
|
| 42 |
+
return history
|
| 43 |
+
|
| 44 |
+
# Default response from the Hugging Face model
|
| 45 |
+
messages = [{"role": "system", "content": "You are a friendly real estate assistant."}]
|
| 46 |
+
for user_message, bot_response in history:
|
| 47 |
+
if user_message:
|
| 48 |
+
messages.append({"role": "user", "content": user_message})
|
| 49 |
+
if bot_response:
|
| 50 |
+
messages.append({"role": "assistant", "content": bot_response})
|
| 51 |
+
|
| 52 |
+
messages.append({"role": "user", "content": message})
|
| 53 |
+
|
| 54 |
+
response = ""
|
| 55 |
+
for chunk in hf_client.chat_completion(messages, stream=True):
|
| 56 |
+
token = chunk.choices[0].delta.content
|
| 57 |
+
response += token
|
| 58 |
+
|
|
|
|
| 59 |
history.append({"role": "assistant", "content": response})
|
| 60 |
return history
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
+
error_message = f"An error occurred: {str(e)}"
|
| 64 |
+
history.append({"role": "assistant", "content": error_message})
|
| 65 |
+
return history
|
| 66 |
+
|
| 67 |
+
# Gradio Interface
|
| 68 |
+
chat_interface = gr.ChatInterface(
|
| 69 |
+
respond,
|
| 70 |
+
chatbot=gr.Chatbot(),
|
| 71 |
+
title="Real Estate Chatbot",
|
| 72 |
+
description="Ask about available apartments or districts in our database.",
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
chat_interface.launch()
|