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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -53
app.py CHANGED
@@ -1,81 +1,37 @@
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 Setup
7
- SERVICE_ACCOUNT_FILE = "sheet-integration-agent@bold-physics-445007-g4.iam.gserviceaccount.com.json"
8
- scopes = ["https://www.googleapis.com/auth/spreadsheets"]
9
- credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=scopes)
10
- client_gs = gspread.authorize(credentials)
11
- spreadsheet = client_gs.open_by_key("1LHgb4eYDM2ImFY3e3h4kPUnDh6oRHpVz2PA29lQ3Xxg")
12
- worksheet = spreadsheet.sheet1
13
-
14
- # Hugging Face Model Setup
15
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
16
-
17
- def get_apartment_data(query):
18
- rows = worksheet.get_all_records()
19
- results = []
20
- for row in rows:
21
- if query.lower() in row["district"].lower() or query.lower() in row["developer"].lower():
22
- results.append(row)
23
- if results:
24
- return "\n".join(
25
- [f"District: {row['district']}, Developer: {row['developer']}, Area: {row['apt_area']} sqm"
26
- for row in results]
27
- )
28
- else:
29
- return "Sorry, no matching apartments found."
30
-
31
  def respond(message, history):
32
  print("Query received:", message)
33
  print("Chat history:", history)
34
 
35
- # Initialize history as a list of dictionaries if empty
36
  if not history:
37
  history = []
38
 
39
  try:
40
- if "district" in message.lower() or "developer" in message.lower():
 
41
  response = get_apartment_data(message)
42
  else:
 
43
  messages = [{"role": "system", "content": "You are a helpful assistant."}]
44
  for entry in history:
45
- if entry["role"] == "user":
46
- messages.append({"role": "user", "content": entry["content"]})
47
- elif entry["role"] == "assistant":
48
- messages.append({"role": "assistant", "content": entry["content"]})
49
 
50
  messages.append({"role": "user", "content": message})
51
 
52
  # Generate response from model
53
- generated_response = client.chat_completion(
54
  messages=messages,
55
  max_tokens=512,
56
  temperature=0.7,
57
  top_p=0.95,
58
  )
59
- response = generated_response["choices"][0]["message"]["content"]
60
 
61
- # Append the new interaction to the history
62
  history.append({"role": "user", "content": message})
63
  history.append({"role": "assistant", "content": response})
64
  return history
65
 
66
  except Exception as e:
67
  print(f"Error processing query: {e}")
68
- return [{"role": "assistant", "content": "An error occurred while processing your query."}]
69
-
70
- # Set up Gradio Chat Interface
71
- chat_interface = gr.ChatInterface(
72
- fn=respond,
73
- examples=[
74
- "What apartments are available in Bostandyk?",
75
- "Tell me about BI Group.",
76
- "What options do you have for developers?",
77
- ],
78
- )
79
-
80
- if __name__ == "__main__":
81
- chat_interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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."}]