Spaces:
Runtime error
Runtime error
File size: 3,060 Bytes
f5e6d7b 06038df f5e6d7b 6446e33 f5e6d7b 6446e33 0466960 6446e33 f5e6d7b 5f76aad 06038df f5e6d7b 6446e33 5f76aad 6446e33 5f76aad 6446e33 5f76aad 6446e33 5f76aad 6446e33 5f76aad 6446e33 5f76aad 6446e33 f5e6d7b 81e39b9 f5e6d7b 06038df | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import gradio as gr
from huggingface_hub import InferenceClient
import gspread
from google.oauth2.service_account import Credentials
# Connecting to Google Sheets
SERVICE_ACCOUNT_FILE = "sheet-integration-agent@bold-physics-445007-g4.iam.gserviceaccount.com.json"
scopes = ["https://www.googleapis.com/auth/spreadsheets"]
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=scopes)
client_gs = gspread.authorize(credentials)
spreadsheet = client_gs.open_by_key("1LHgb4eYDM2ImFY3e3h4kPUnDh6oRHpVz2PA29lQ3Xxg")
worksheet = spreadsheet.sheet1
# Connecting to the zephyr-7b-beta model via Inference API
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# Function to retrieve data from Google Sheets
def get_apartment_data(query):
rows = worksheet.get_all_records()
results = []
for row in rows:
# Basic check if the query is found in "district" or "developer"
if query.lower() in row["district"].lower() or query.lower() in row["developer"].lower():
results.append(row)
if results:
return "\n".join(
[f"District: {row['district']}, Developer: {row['developer']}, Area: {row['apt_area']} m²"
for row in results]
)
else:
return "Sorry, no matching options found."
# Function to handle queries
def respond(message, history):
"""
message (str): latest user message
history (list): the entire conversation so far, as a list of dicts like:
[
{"role": "user", "content": "..."},
{"role": "assistant", "content": "..."},
...
]
Returns a single string (the assistant's answer).
"""
print("Query received:", message)
print("Chat history:", history)
try:
# If the query is about district or developer, fetch from Sheets
if "district" in message.lower() or "developer" in message.lower():
response = get_apartment_data(message)
else:
# Build messages from history for context
conversation = []
for turn in history:
conversation.append({"role": turn["role"], "content": turn["content"]})
# Add the new user message
conversation.append({"role": "user", "content": message})
# Query Zephyr
generated_response = client.chat_completion(
messages=conversation,
max_tokens=512,
temperature=0.7,
top_p=0.95,
)
response = generated_response["choices"][0]["message"]["content"]
# Just return the assistant's text
return response
except Exception as e:
print(f"Error processing query: {e}")
return "An error occurred while processing your query."
# Gradio Interface
chat_interface = gr.ChatInterface(
fn=respond,
title="Apartment Agent",
description="This chatbot can provide apartment-related information and answer your questions."
)
if __name__ == "__main__":
chat_interface.launch() |