Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234540 unmodified lines46474849505152535455565758import gradio as grimport os# Install the groq package if it is not installedtry:40 unmodified lineschatbot = SimpleChatBot()def respond(message, chat_history):chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]response = chatbot.get_response(message, chat_history)chat_history.append({"role": "user", "content": message})chat_history.append({"role": "assistant", "content": response})return chat_history, ""with gr.Blocks(title="簡單的Gradio聊天機器人") as demo:gr.Markdown("# 簡單的Gradio聊天機器人")1234567840 unmodified lines4950515253545556575859606162636465666768697071727374757677787980818283848586import gradio as grimport osimport csvimport requestsfrom datetime import datetime# Install the groq package if it is not installedtry:40 unmodified lineschatbot = SimpleChatBot()# Function to log chat history to Google Sheetdef log_to_sheet(user_message, bot_response):# Prepare the URL for Google Sheets API using the shared linksheet_url = "https://docs.google.com/spreadsheets/d/1BQ23fiiGu5wN4RmRC4XmrKS6qCWHuymlbsoLEEmEN_4/gviz/tq?tqx=out:csv"# Prepare the data rowtimestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")row = [timestamp, user_message, bot_response]# Append the row to the sheet using HTTP requeststry:response = requests.post(sheet_url, data={'append': row})if response.status_code == 200:print("Logged to Google Sheet successfully.")else:print(f"Failed to log to Google Sheet: {response.status_code}")except Exception as e:print(f"Error while logging to Google Sheet: {e}")# Chatbot response function with loggingdef respond(message, chat_history):chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history]response = chatbot.get_response(message, chat_history)chat_history.append({"role": "user", "content": message})chat_history.append({"role": "assistant", "content": response})# Log to Google Sheetlog_to_sheet(message, response)return chat_history, ""# Gradio interfacewith gr.Blocks(title="簡單的Gradio聊天機器人") as demo:gr.Markdown("# 簡單的Gradio聊天機器人")
app.py+28