Salmetov commited on
Commit
06038df
·
verified ·
1 Parent(s): f5e6d7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -58
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
- # 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()
 
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()