Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
import requests
|
|
|
5 |
|
6 |
groq_client = Groq(
|
7 |
api_key=os.environ.get("GROQ_API_KEY"),
|
@@ -10,6 +11,52 @@ groq_client = Groq(
|
|
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 = {
|
@@ -17,6 +64,9 @@ def store_conversation(user_input, bot_response):
|
|
17 |
"Content-Type": "application/json",
|
18 |
"Notion-Version": "2022-06-28"
|
19 |
}
|
|
|
|
|
|
|
20 |
data = {
|
21 |
"children": [
|
22 |
{
|
@@ -41,9 +91,16 @@ def store_conversation(user_input, bot_response):
|
|
41 |
"object": "block",
|
42 |
"type": "paragraph",
|
43 |
"paragraph": {
|
44 |
-
"rich_text": [
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
}
|
46 |
-
}
|
|
|
47 |
]
|
48 |
}
|
49 |
|
@@ -74,7 +131,8 @@ def chat_with_groq(user_input, additional_context=None):
|
|
74 |
demo = gr.ChatInterface(fn=chat_with_groq,
|
75 |
textbox=gr.Textbox(placeholder="Ask me any question"),
|
76 |
title="Hey NOPE", theme="Monochrome",
|
77 |
-
description="Welcome to the world of NOPE",
|
|
|
78 |
retry_btn=None,
|
79 |
undo_btn="Delete Previous",
|
80 |
clear_btn="Clear",)
|
|
|
2 |
import gradio as gr
|
3 |
from groq import Groq
|
4 |
import requests
|
5 |
+
import re
|
6 |
|
7 |
groq_client = Groq(
|
8 |
api_key=os.environ.get("GROQ_API_KEY"),
|
|
|
11 |
NOTION_TOKEN = os.environ.get("NOTION_TOKEN")
|
12 |
NOTION_PAGE_ID = "4fc0a081f0a84257879d6f7638e368b9" # Replace with your actual page ID
|
13 |
|
14 |
+
def format_text_for_notion(text):
|
15 |
+
# Split the text into lines
|
16 |
+
lines = text.split('\n')
|
17 |
+
formatted_text = []
|
18 |
+
|
19 |
+
for line in lines:
|
20 |
+
# Check if the line is a bullet point
|
21 |
+
if line.strip().startswith('•') or line.strip().startswith('-'):
|
22 |
+
formatted_text.append({
|
23 |
+
"type": "bulleted_list_item",
|
24 |
+
"bulleted_list_item": {
|
25 |
+
"rich_text": [{"type": "text", "text": {"content": line.strip()[2:].strip()}}]
|
26 |
+
}
|
27 |
+
})
|
28 |
+
else:
|
29 |
+
# Check for bold text (assuming it's wrapped in ** or __)
|
30 |
+
bold_parts = re.split(r'(\*\*.*?\*\*|__.*?__)', line)
|
31 |
+
rich_text = []
|
32 |
+
for part in bold_parts:
|
33 |
+
if part.startswith('**') and part.endswith('**'):
|
34 |
+
rich_text.append({
|
35 |
+
"type": "text",
|
36 |
+
"text": {"content": part[2:-2]},
|
37 |
+
"annotations": {"bold": True}
|
38 |
+
})
|
39 |
+
elif part.startswith('__') and part.endswith('__'):
|
40 |
+
rich_text.append({
|
41 |
+
"type": "text",
|
42 |
+
"text": {"content": part[2:-2]},
|
43 |
+
"annotations": {"bold": True}
|
44 |
+
})
|
45 |
+
elif part:
|
46 |
+
rich_text.append({
|
47 |
+
"type": "text",
|
48 |
+
"text": {"content": part}
|
49 |
+
})
|
50 |
+
|
51 |
+
formatted_text.append({
|
52 |
+
"type": "paragraph",
|
53 |
+
"paragraph": {
|
54 |
+
"rich_text": rich_text
|
55 |
+
}
|
56 |
+
})
|
57 |
+
|
58 |
+
return formatted_text
|
59 |
+
|
60 |
def store_conversation(user_input, bot_response):
|
61 |
url = f"https://api.notion.com/v1/blocks/{NOTION_PAGE_ID}/children"
|
62 |
headers = {
|
|
|
64 |
"Content-Type": "application/json",
|
65 |
"Notion-Version": "2022-06-28"
|
66 |
}
|
67 |
+
|
68 |
+
formatted_bot_response = format_text_for_notion(bot_response)
|
69 |
+
|
70 |
data = {
|
71 |
"children": [
|
72 |
{
|
|
|
91 |
"object": "block",
|
92 |
"type": "paragraph",
|
93 |
"paragraph": {
|
94 |
+
"rich_text": [
|
95 |
+
{
|
96 |
+
"type": "text",
|
97 |
+
"text": {"content": "Bot: "},
|
98 |
+
"annotations": {"bold": True}
|
99 |
+
}
|
100 |
+
]
|
101 |
}
|
102 |
+
},
|
103 |
+
*formatted_bot_response
|
104 |
]
|
105 |
}
|
106 |
|
|
|
131 |
demo = gr.ChatInterface(fn=chat_with_groq,
|
132 |
textbox=gr.Textbox(placeholder="Ask me any question"),
|
133 |
title="Hey NOPE", theme="Monochrome",
|
134 |
+
description="Welcome to the world of NOPE",
|
135 |
+
examples=["Need some content Idea", "Generate some Thumbnail Text"],
|
136 |
retry_btn=None,
|
137 |
undo_btn="Delete Previous",
|
138 |
clear_btn="Clear",)
|