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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -30
app.py CHANGED
@@ -1,37 +1,76 @@
1
- def respond(message, history):
2
- print("Query received:", message)
3
- print("Chat history:", history)
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Ensure history is a list of dictionaries
6
- if not history:
7
- history = []
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  try:
10
- # Check for specific keywords to invoke Google Sheets query
11
- if "bostandyk" in message.lower() or "developer" in message.lower():
12
- response = get_apartment_data(message)
13
- else:
14
- # Process the query using the model
15
- messages = [{"role": "system", "content": "You are a helpful assistant."}]
16
- for entry in history:
17
- messages.append({"role": entry["role"], "content": entry["content"]})
18
-
19
- messages.append({"role": "user", "content": message})
20
-
21
- # Generate response from model
22
- model_response = client.chat_completion(
23
- messages=messages,
24
- max_tokens=512,
25
- temperature=0.7,
26
- top_p=0.95,
27
- )
28
- response = model_response["choices"][0]["message"]["content"]
29
-
30
- # Append the interaction to the history
31
- history.append({"role": "user", "content": message})
32
  history.append({"role": "assistant", "content": response})
33
  return history
34
 
35
  except Exception as e:
36
- print(f"Error processing query: {e}")
37
- return [{"role": "assistant", "content": "An error occurred while processing your query."}]
 
 
 
 
 
 
 
 
 
 
 
 
 
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()