Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,56 +16,72 @@ worksheet = spreadsheet.sheet1
|
|
| 16 |
|
| 17 |
# Функция для поиска данных в Google Sheets
|
| 18 |
def get_apartment_data(query):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Функция для обработки запросов и ответов
|
| 28 |
-
def respond(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
system_message,
|
| 32 |
-
max_tokens,
|
| 33 |
-
temperature,
|
| 34 |
-
top_p,
|
| 35 |
-
):
|
| 36 |
-
# Проверка на запрос для Google Sheets
|
| 37 |
-
if "квартира" in message.lower() or "район" in message.lower():
|
| 38 |
-
response = get_apartment_data(message)
|
| 39 |
-
history.append((message, response))
|
| 40 |
-
return "", history
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
messages.append({"role": "user", "content": message})
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Интерфейс Gradio
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
-
respond,
|
| 69 |
additional_inputs=[
|
| 70 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 71 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
| 16 |
|
| 17 |
# Функция для поиска данных в Google Sheets
|
| 18 |
def get_apartment_data(query):
|
| 19 |
+
try:
|
| 20 |
+
rows = worksheet.get_all_records()
|
| 21 |
+
results = []
|
| 22 |
+
for row in rows:
|
| 23 |
+
if query.lower() in row["district"].lower() or query.lower() in row["developer"].lower():
|
| 24 |
+
results.append({
|
| 25 |
+
"district": row["district"],
|
| 26 |
+
"developer": row["developer"],
|
| 27 |
+
"project_class": row["project_class"],
|
| 28 |
+
"apt_area": row["apt_area"]
|
| 29 |
+
})
|
| 30 |
+
return results
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Ошибка при работе с Google Sheets: {e}")
|
| 33 |
+
return []
|
| 34 |
|
| 35 |
# Функция для обработки запросов и ответов
|
| 36 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 37 |
+
print("Получен запрос:", message) # Логирование для отладки
|
| 38 |
+
print("История общения:", history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
# Проверка на запрос для Google Sheets
|
| 42 |
+
if "район" in message.lower() or "застройщик" in message.lower():
|
| 43 |
+
response = get_apartment_data(message)
|
| 44 |
+
if response:
|
| 45 |
+
result = "\n".join([
|
| 46 |
+
f"Район: {row['district']}, Застройщик: {row['developer']}, "
|
| 47 |
+
f"Класс: {row['project_class']}, Площадь: {row['apt_area']}"
|
| 48 |
+
for row in response
|
| 49 |
+
])
|
| 50 |
+
history.append((message, result))
|
| 51 |
+
yield "", history
|
| 52 |
+
else:
|
| 53 |
+
history.append((message, "Извините, подходящих вариантов не найдено."))
|
| 54 |
+
yield "", history
|
| 55 |
+
else:
|
| 56 |
+
# Генерация ответа от модели
|
| 57 |
+
messages = [{"role": "system", "content": system_message}]
|
| 58 |
+
for user_message, bot_response in history:
|
| 59 |
+
if user_message:
|
| 60 |
+
messages.append({"role": "user", "content": user_message})
|
| 61 |
+
if bot_response:
|
| 62 |
+
messages.append({"role": "assistant", "content": bot_response})
|
| 63 |
|
| 64 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
| 65 |
|
| 66 |
+
response = ""
|
| 67 |
+
for message in client.chat_completion(
|
| 68 |
+
messages,
|
| 69 |
+
max_tokens=max_tokens,
|
| 70 |
+
stream=True,
|
| 71 |
+
temperature=temperature,
|
| 72 |
+
top_p=top_p,
|
| 73 |
+
):
|
| 74 |
+
token = message.choices[0].delta.content
|
| 75 |
+
response += token
|
| 76 |
+
yield response
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Ошибка обработки запроса: {e}")
|
| 79 |
+
history.append((message, "Произошла ошибка при обработке вашего запроса."))
|
| 80 |
+
yield "", history
|
| 81 |
|
| 82 |
# Интерфейс Gradio
|
| 83 |
demo = gr.ChatInterface(
|
| 84 |
+
fn=respond,
|
| 85 |
additional_inputs=[
|
| 86 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 87 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|