Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from datetime import datetime
|
4 |
+
|
5 |
+
# Ensure Groq is installed and imported
|
6 |
+
try:
|
7 |
+
from groq import Groq
|
8 |
+
except ImportError:
|
9 |
+
os.system('pip install groq')
|
10 |
+
from groq import Groq
|
11 |
+
|
12 |
+
# Ensure Notion client is installed and imported
|
13 |
+
try:
|
14 |
+
from notion_client import Client
|
15 |
+
except ImportError:
|
16 |
+
os.system('pip install notion-client')
|
17 |
+
from notion_client import Client
|
18 |
+
|
19 |
+
# Retrieve API keys and database ID from environment variables
|
20 |
+
groq_api_key = os.getenv("groq_key")
|
21 |
+
notion_api_key = os.getenv("NOTION_API_KEY")
|
22 |
+
notion_db_id = os.getenv("NOTION_DB_ID")
|
23 |
+
|
24 |
+
if not groq_api_key:
|
25 |
+
raise EnvironmentError("API key for Groq not found in environment variables.")
|
26 |
+
if not notion_api_key:
|
27 |
+
raise EnvironmentError("API key for Notion not found in environment variables.")
|
28 |
+
if not notion_db_id:
|
29 |
+
raise EnvironmentError("Database ID for Notion not found in environment variables.")
|
30 |
+
|
31 |
+
groq_client = Groq(api_key=groq_api_key)
|
32 |
+
notion_client = Client(auth=notion_api_key)
|
33 |
+
|
34 |
+
# Function to log to Notion
|
35 |
+
def log_to_notion(name, user_input, bot_response):
|
36 |
+
try:
|
37 |
+
notion_client.pages.create(
|
38 |
+
parent={"database_id": notion_db_id},
|
39 |
+
properties={
|
40 |
+
"Name": {"title": [{"text": {"content": name}}]},
|
41 |
+
"Timestamp": {"date": {"start": datetime.now().isoformat() }},
|
42 |
+
"User Input": {"rich_text": [{"text": {"content": user_input}}]},
|
43 |
+
"Bot Response": {"rich_text": [{"text": {"content": bot_response}}]},
|
44 |
+
}
|
45 |
+
)
|
46 |
+
except Exception as e:
|
47 |
+
print(f"Failed to log to Notion: {e}")
|
48 |
+
|
49 |
+
# Function to interact with Groq
|
50 |
+
async def chatbot_response(name, message, history):
|
51 |
+
"""Handles user input and retrieves a response from the Groq API."""
|
52 |
+
try:
|
53 |
+
# Send message to Groq with role: system
|
54 |
+
response = groq_client.chat(
|
55 |
+
messages=[
|
56 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
57 |
+
*[{"role": "user", "content": msg[0]} for msg in history],
|
58 |
+
{"role": "user", "content": message}
|
59 |
+
]
|
60 |
+
)
|
61 |
+
reply = response["choices"][0]["message"]["content"]
|
62 |
+
history.append((message, reply))
|
63 |
+
|
64 |
+
# Log interaction to Notion
|
65 |
+
log_to_notion(name, message, reply)
|
66 |
+
|
67 |
+
return "", history
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error: {str(e)}", history
|
70 |
+
|
71 |
+
# Gradio interface
|
72 |
+
def main():
|
73 |
+
chatbot = gr.Chatbot()
|
74 |
+
name = gr.Textbox(label="Name", placeholder="Enter your name here...")
|
75 |
+
msg = gr.Textbox(placeholder="Enter your message here...")
|
76 |
+
clear = gr.Button("Clear Chat")
|
77 |
+
|
78 |
+
def reset():
|
79 |
+
return "", []
|
80 |
+
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
gr.Markdown("# Groq Chatbot with Gradio and Notion Logging")
|
83 |
+
with gr.Row():
|
84 |
+
name.render()
|
85 |
+
msg.render()
|
86 |
+
msg.submit(chatbot_response, [name, msg, chatbot], [msg, chatbot])
|
87 |
+
clear.click(reset, None, chatbot)
|
88 |
+
|
89 |
+
demo.launch()
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
main()
|