File size: 1,887 Bytes
0b090fd
 
 
 
 
 
 
 
 
a2afe38
0b090fd
 
 
887ef85
 
a2afe38
 
 
 
 
 
 
 
 
 
 
0b090fd
 
 
 
 
 
 
 
 
 
 
 
a2afe38
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
import os
import gradio as gr
from groq import Groq
from notion_client import Client

groq_client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

notion_client = Client(auth=os.environ.get("NOTION_TOKEN"))
notion_database_id = "4fc0a081f0a84257879d6f7638e368b9"

def store_conversation(user_input, bot_response):
    # Truncate bot_response if it exceeds 2000 characters
    truncated_response = bot_response[:1997] + "..." if len(bot_response) > 2000 else bot_response
    try:
        notion_client.pages.create(
            parent={"database_id": notion_database_id},
            properties={
                "User Input": {"title": [{"text": {"content": user_input}}]},
                "Bot Response": {"rich_text": [{"text": {"content": truncated_response}}]}
            }
        )
        print("Conversation stored successfully")
    except Exception as e:
        print(f"Error storing conversation: {str(e)}")

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__":
    demo.launch()