adriiita commited on
Commit
22fe211
·
verified ·
1 Parent(s): 36d8e38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -8,33 +8,43 @@ groq_client = Groq(
8
  )
9
 
10
  NOTION_TOKEN = os.environ.get("NOTION_TOKEN")
11
- NOTION_DATABASE_ID = "4fc0a081f0a84257879d6f7638e368b9"
12
 
13
  def store_conversation(user_input, bot_response):
14
- # Truncate bot_response if it exceeds 2000 characters
15
- truncated_response = bot_response[:1997] + "..." if len(bot_response) > 2000 else bot_response
16
-
17
- url = f"https://api.notion.com/v1/pages"
18
  headers = {
19
  "Authorization": f"Bearer {NOTION_TOKEN}",
20
  "Content-Type": "application/json",
21
  "Notion-Version": "2022-06-28"
22
  }
23
  data = {
24
- "parent": {"database_id": NOTION_DATABASE_ID},
25
- "properties": {
26
- "User Input": {"title": [{"text": {"content": user_input}}]},
27
- "Bot Response": {"rich_text": [{"text": {"content": truncated_response}}]}
28
- }
 
 
 
 
 
 
 
 
 
 
 
29
  }
30
 
31
  try:
32
- response = requests.post(url, headers=headers, json=data)
33
  response.raise_for_status()
34
  print("Conversation stored successfully")
35
  except requests.exceptions.RequestException as e:
36
  print(f"Error storing conversation: {str(e)}")
37
- print("Make sure the Notion database is shared with your integration and the database ID is correct.")
 
 
38
 
39
  def chat_with_groq(user_input, additional_context=None):
40
  chat_completion = groq_client.chat.completions.create(
@@ -60,6 +70,6 @@ demo = gr.ChatInterface(fn=chat_with_groq,
60
  clear_btn="Clear",)
61
 
62
  if __name__ == "__main__":
63
- print(f"Using Notion database ID: {NOTION_DATABASE_ID}")
64
- print("Make sure the Notion database is shared with your integration.")
65
  demo.launch()
 
8
  )
9
 
10
  NOTION_TOKEN = os.environ.get("NOTION_TOKEN")
11
+ NOTION_PAGE_ID = "4fc0a081f0a84257879d6f7638e368b9" # Replace with your actual page ID
12
 
13
  def store_conversation(user_input, bot_response):
14
+ url = f"https://api.notion.com/v1/blocks/{NOTION_PAGE_ID}/children"
 
 
 
15
  headers = {
16
  "Authorization": f"Bearer {NOTION_TOKEN}",
17
  "Content-Type": "application/json",
18
  "Notion-Version": "2022-06-28"
19
  }
20
  data = {
21
+ "children": [
22
+ {
23
+ "object": "block",
24
+ "type": "paragraph",
25
+ "paragraph": {
26
+ "rich_text": [{"type": "text", "text": {"content": f"User: {user_input}"}}]
27
+ }
28
+ },
29
+ {
30
+ "object": "block",
31
+ "type": "paragraph",
32
+ "paragraph": {
33
+ "rich_text": [{"type": "text", "text": {"content": f"Bot: {bot_response[:1997]}..."}}]
34
+ }
35
+ }
36
+ ]
37
  }
38
 
39
  try:
40
+ response = requests.patch(url, headers=headers, json=data)
41
  response.raise_for_status()
42
  print("Conversation stored successfully")
43
  except requests.exceptions.RequestException as e:
44
  print(f"Error storing conversation: {str(e)}")
45
+ print(f"Response status code: {e.response.status_code}")
46
+ print(f"Response content: {e.response.content}")
47
+ print("Make sure the Notion page is shared with your integration and the page ID is correct.")
48
 
49
  def chat_with_groq(user_input, additional_context=None):
50
  chat_completion = groq_client.chat.completions.create(
 
70
  clear_btn="Clear",)
71
 
72
  if __name__ == "__main__":
73
+ print(f"Using Notion page ID: {NOTION_PAGE_ID}")
74
+ print("Make sure the Notion page is shared with your integration.")
75
  demo.launch()