Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import gspread
|
| 4 |
from google.oauth2.service_account import Credentials
|
| 5 |
|
| 6 |
-
#
|
| 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)
|
|
@@ -11,10 +11,9 @@ client_gs = gspread.authorize(credentials)
|
|
| 11 |
spreadsheet = client_gs.open_by_key("1LHgb4eYDM2ImFY3e3h4kPUnDh6oRHpVz2PA29lQ3Xxg")
|
| 12 |
worksheet = spreadsheet.sheet1
|
| 13 |
|
| 14 |
-
#
|
| 15 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 16 |
|
| 17 |
-
# Функция для получения данных из Google Sheets
|
| 18 |
def get_apartment_data(query):
|
| 19 |
rows = worksheet.get_all_records()
|
| 20 |
results = []
|
|
@@ -23,26 +22,23 @@ def get_apartment_data(query):
|
|
| 23 |
results.append(row)
|
| 24 |
if results:
|
| 25 |
return "\n".join(
|
| 26 |
-
[f"
|
| 27 |
for row in results]
|
| 28 |
)
|
| 29 |
else:
|
| 30 |
-
return "
|
| 31 |
|
| 32 |
-
# Функция для обработки запросов
|
| 33 |
def respond(message, history):
|
| 34 |
-
print("
|
| 35 |
-
print("
|
| 36 |
|
| 37 |
if not history:
|
| 38 |
history = []
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
if "район" in message.lower() or "застройщик" in message.lower():
|
| 43 |
response = get_apartment_data(message)
|
| 44 |
else:
|
| 45 |
-
# Обработка запроса через модель
|
| 46 |
messages = [{"role": "user", "content": message}]
|
| 47 |
for h in history:
|
| 48 |
messages.append(h)
|
|
@@ -54,21 +50,17 @@ def respond(message, history):
|
|
| 54 |
)
|
| 55 |
response = generated_response["choices"][0]["message"]["content"]
|
| 56 |
|
| 57 |
-
# Формирование корректной истории общения
|
| 58 |
history.append({"role": "user", "content": message})
|
| 59 |
history.append({"role": "assistant", "content": response})
|
| 60 |
return response, history
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
-
print(f"
|
| 64 |
-
history.append({"role": "assistant", "content": "
|
| 65 |
-
return "
|
| 66 |
|
| 67 |
-
# Интерфейс Gradio
|
| 68 |
chat_interface = gr.ChatInterface(
|
| 69 |
-
respond
|
| 70 |
-
input_label="Ваш вопрос",
|
| 71 |
-
output_label="Ответ",
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 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)
|
|
|
|
| 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 = []
|
|
|
|
| 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 |
if not history:
|
| 36 |
history = []
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
if "district" in message.lower() or "developer" in message.lower():
|
|
|
|
| 40 |
response = get_apartment_data(message)
|
| 41 |
else:
|
|
|
|
| 42 |
messages = [{"role": "user", "content": message}]
|
| 43 |
for h in history:
|
| 44 |
messages.append(h)
|
|
|
|
| 50 |
)
|
| 51 |
response = generated_response["choices"][0]["message"]["content"]
|
| 52 |
|
|
|
|
| 53 |
history.append({"role": "user", "content": message})
|
| 54 |
history.append({"role": "assistant", "content": response})
|
| 55 |
return response, history
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
print(f"Error processing query: {e}")
|
| 59 |
+
history.append({"role": "assistant", "content": "An error occurred while processing your query."})
|
| 60 |
+
return "An error occurred while processing your query.", history
|
| 61 |
|
|
|
|
| 62 |
chat_interface = gr.ChatInterface(
|
| 63 |
+
respond
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|