Spaces:
Sleeping
Sleeping
File size: 4,959 Bytes
0b090fd 36d8e38 71b6955 0b090fd 36d8e38 22fe211 0b090fd 71b6955 0b090fd 22fe211 36d8e38 71b6955 36d8e38 22fe211 d51c28f 22fe211 71b6955 22fe211 71b6955 22fe211 36d8e38 a2afe38 22fe211 36d8e38 a2afe38 36d8e38 a2afe38 22fe211 0b090fd a2afe38 0b090fd 71b6955 0b090fd 22fe211 0b090fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
import os
import gradio as gr
from groq import Groq
import requests
import re
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 format_text_for_notion(text):
# Split the text into lines
lines = text.split('\n')
formatted_text = []
for line in lines:
# Check if the line is a bullet point
if line.strip().startswith('•') or line.strip().startswith('-'):
formatted_text.append({
"type": "bulleted_list_item",
"bulleted_list_item": {
"rich_text": [{"type": "text", "text": {"content": line.strip()[2:].strip()}}]
}
})
else:
# Check for bold text (assuming it's wrapped in ** or __)
bold_parts = re.split(r'(\*\*.*?\*\*|__.*?__)', line)
rich_text = []
for part in bold_parts:
if part.startswith('**') and part.endswith('**'):
rich_text.append({
"type": "text",
"text": {"content": part[2:-2]},
"annotations": {"bold": True}
})
elif part.startswith('__') and part.endswith('__'):
rich_text.append({
"type": "text",
"text": {"content": part[2:-2]},
"annotations": {"bold": True}
})
elif part:
rich_text.append({
"type": "text",
"text": {"content": part}
})
formatted_text.append({
"type": "paragraph",
"paragraph": {
"rich_text": rich_text
}
})
return formatted_text
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"
}
formatted_bot_response = format_text_for_notion(bot_response)
data = {
"children": [
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {"content": "User: "},
"annotations": {"bold": True}
},
{
"type": "text",
"text": {"content": user_input},
"annotations": {"bold": True}
}
]
}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {"content": "Bot: "},
"annotations": {"bold": True}
}
]
}
},
*formatted_bot_response
]
}
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",
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() |