Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,56 @@
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 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
|
| 28 |
results.append(row)
|
| 29 |
-
if results
|
| 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 |
-
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 |
-
|
| 70 |
-
chatbot=gr.Chatbot(),
|
| 71 |
-
title="
|
| 72 |
-
description="Ask about available apartments or
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
chat_interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
import gspread
|
| 4 |
from google.oauth2.service_account import Credentials
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Google Sheets integration setup
|
| 7 |
+
SERVICE_ACCOUNT_FILE = "path_to_your_service_account.json"
|
| 8 |
+
SPREADSHEET_ID = "your_spreadsheet_id"
|
| 9 |
scopes = ["https://www.googleapis.com/auth/spreadsheets"]
|
| 10 |
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=scopes)
|
| 11 |
client = gspread.authorize(credentials)
|
| 12 |
worksheet = client.open_by_key(SPREADSHEET_ID).sheet1
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def get_apartment_data(query):
|
| 15 |
+
"""Fetches relevant apartment data from Google Sheets."""
|
|
|
|
|
|
|
| 16 |
rows = worksheet.get_all_records()
|
| 17 |
results = []
|
| 18 |
for row in rows:
|
| 19 |
+
if query.lower() in row["district"].lower() or query.lower() in row["developer"].lower():
|
| 20 |
results.append(row)
|
| 21 |
+
return results if results else "Sorry, no matching apartments found."
|
| 22 |
+
|
| 23 |
+
# Hugging Face model setup
|
| 24 |
+
hf_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 25 |
+
|
| 26 |
+
def process_query(message, history):
|
| 27 |
+
"""Processes the user's query and returns a response."""
|
| 28 |
+
# Check for specific queries (e.g., related to apartments)
|
| 29 |
+
if "apartment" in message.lower() or "district" in message.lower():
|
| 30 |
+
result = get_apartment_data(message)
|
| 31 |
+
return {"role": "assistant", "content": str(result)}
|
| 32 |
+
|
| 33 |
+
# Default: Handle with Hugging Face model
|
| 34 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 35 |
+
for msg in history:
|
| 36 |
+
messages.append(msg)
|
| 37 |
+
messages.append({"role": "user", "content": message})
|
| 38 |
+
|
| 39 |
+
# Fetch response from the Hugging Face model
|
| 40 |
+
response = ""
|
| 41 |
+
for res in hf_client.chat_completion(messages, stream=True):
|
| 42 |
+
token = res.choices[0].delta.content
|
| 43 |
+
response += token
|
| 44 |
+
|
| 45 |
+
return {"role": "assistant", "content": response}
|
| 46 |
+
|
| 47 |
+
# Gradio ChatInterface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
chat_interface = gr.ChatInterface(
|
| 49 |
+
process_query,
|
| 50 |
+
chatbot=gr.Chatbot(label="Your Assistant"),
|
| 51 |
+
title="Apartment Finder Bot",
|
| 52 |
+
description="Ask about available apartments or anything else.",
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
chat_interface.launch()
|