Spaces:
Runtime error
Runtime error
Download app.py from RealEstateAgent/RealEstateAgentBot: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/RealEstateAgent/RealEstateAgentBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/RealEstateAgent/RealEstateAgentBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/RealEstateAgent/RealEstateAgentBot/resolve/main/app.py
3.06 kB
| 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() |