ChatbotNotionDB / app.py
adriiita's picture
Update app.py
22fe211 verified
raw
history blame
2.69 kB
import os
import gradio as gr
from groq import Groq
import requests
groq_client = Groq(
api_key=os.environ.get("GROQ_API_KEY"),
)
NOTION_TOKEN = os.environ.get("NOTION_TOKEN")
NOTION_PAGE_ID = "4fc0a081f0a84257879d6f7638e368b9" # Replace with your actual page ID
def store_conversation(user_input, bot_response):
url = f"https://api.notion.com/v1/blocks/{NOTION_PAGE_ID}/children"
headers = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
data = {
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": f"User: {user_input}"}}]
}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": f"Bot: {bot_response[:1997]}..."}}]
}
}
]
}
try:
response = requests.patch(url, headers=headers, json=data)
response.raise_for_status()
print("Conversation stored successfully")
except requests.exceptions.RequestException as e:
print(f"Error storing conversation: {str(e)}")
print(f"Response status code: {e.response.status_code}")
print(f"Response content: {e.response.content}")
print("Make sure the Notion page is shared with your integration and the page ID is correct.")
def chat_with_groq(user_input, additional_context=None):
chat_completion = groq_client.chat.completions.create(
messages=[
{
"role": "user",
"content": user_input,
}
],
model="llama-3.1-8b-instant",
)
bot_response = chat_completion.choices[0].message.content
store_conversation(user_input, bot_response)
return bot_response
demo = gr.ChatInterface(fn=chat_with_groq,
textbox=gr.Textbox(placeholder="Ask me any question"),
title="Hey NOPE", theme="Monochrome",
description="Welcome to the world of NOPE",
examples=["Need some content Idea", "Generate some Thumbnail Text"],
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",)
if __name__ == "__main__":
print(f"Using Notion page ID: {NOTION_PAGE_ID}")
print("Make sure the Notion page is shared with your integration.")
demo.launch()