Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Install the groq package if it is not installed
|
| 5 |
try:
|
|
@@ -46,13 +49,38 @@ class SimpleChatBot:
|
|
| 46 |
|
| 47 |
chatbot = SimpleChatBot()
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def respond(message, chat_history):
|
| 50 |
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
| 51 |
response = chatbot.get_response(message, chat_history)
|
| 52 |
chat_history.append({"role": "user", "content": message})
|
| 53 |
chat_history.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return chat_history, ""
|
| 55 |
|
|
|
|
| 56 |
with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
|
| 57 |
gr.Markdown("# 簡單的Gradio聊天機器人")
|
| 58 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import csv
|
| 4 |
+
import requests
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# Install the groq package if it is not installed
|
| 8 |
try:
|
|
|
|
| 49 |
|
| 50 |
chatbot = SimpleChatBot()
|
| 51 |
|
| 52 |
+
# Function to log chat history to Google Sheet
|
| 53 |
+
def log_to_sheet(user_message, bot_response):
|
| 54 |
+
# Prepare the URL for Google Sheets API using the shared link
|
| 55 |
+
sheet_url = "https://docs.google.com/spreadsheets/d/1BQ23fiiGu5wN4RmRC4XmrKS6qCWHuymlbsoLEEmEN_4/gviz/tq?tqx=out:csv"
|
| 56 |
+
|
| 57 |
+
# Prepare the data row
|
| 58 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 59 |
+
row = [timestamp, user_message, bot_response]
|
| 60 |
+
|
| 61 |
+
# Append the row to the sheet using HTTP requests
|
| 62 |
+
try:
|
| 63 |
+
response = requests.post(sheet_url, data={'append': row})
|
| 64 |
+
if response.status_code == 200:
|
| 65 |
+
print("Logged to Google Sheet successfully.")
|
| 66 |
+
else:
|
| 67 |
+
print(f"Failed to log to Google Sheet: {response.status_code}")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error while logging to Google Sheet: {e}")
|
| 70 |
+
|
| 71 |
+
# Chatbot response function with logging
|
| 72 |
def respond(message, chat_history):
|
| 73 |
chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]
|
| 74 |
response = chatbot.get_response(message, chat_history)
|
| 75 |
chat_history.append({"role": "user", "content": message})
|
| 76 |
chat_history.append({"role": "assistant", "content": response})
|
| 77 |
+
|
| 78 |
+
# Log to Google Sheet
|
| 79 |
+
log_to_sheet(message, response)
|
| 80 |
+
|
| 81 |
return chat_history, ""
|
| 82 |
|
| 83 |
+
# Gradio interface
|
| 84 |
with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:
|
| 85 |
gr.Markdown("# 簡單的Gradio聊天機器人")
|
| 86 |
|