File size: 2,147 Bytes
0f06d50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
import google.generativeai as genai
from dotenv import load_dotenv

load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
model = genai.GenerativeModel("gemini-2.0-flash")


def chatBot():
    chatHistory = []
    chatHistory.append(
        {
            "role": "user",
            "parts": "Imagine you're ByteBot, the go-to AI assistant for mastering Computer Science concepts. Your mission: break down intricate CS topics into easily digestible insights, using simple language and relatable examples that even a 5-year-old can grasp. Always ensure to gauge user understanding by asking for feedback. If a question veers off-topic, politely redirect the user, emphasizing the focus on CS-related inquiries to provide the best assistance possible. With ByteBot by your side, navigating the complexities of Computer Science has never been more straightforward.",
        }
    )
    startPrompt = model.generate_content(chatHistory)
    chatHistory.append({"role": "model", "parts": [startPrompt.text]})
    return chatHistory


def response(text, history):
    chatHistory = history
    history = chatBot()
    for human, assistant in chatHistory:
        history.append({"role": "user", "parts": [human]})
        history.append({"role": "model", "parts": [assistant]})
    history.append({"role": "user", "parts": [text]})

    response = model.generate_content(history)
    history.append({"role": "model", "parts": [response.text]})
    return response.text


app = gr.ChatInterface(
    fn=response,
    chatbot=gr.Chatbot(
        show_copy_button="True",
    ),
    theme=gr.themes.Soft(
        primary_hue="sky",
        secondary_hue="sky",
        neutral_hue="stone",
        font=[
            gr.themes.GoogleFont("Montserrat"),
            gr.themes.GoogleFont("Poppins"),
            "system-ui",
            "sans-serif",
        ],
        font_mono=[
            gr.themes.GoogleFont("Poppins"),
            "ui-monospace",
            "Consolas",
            "monospace",
        ],
    ),
    title="🤖 ByteBot: Your CS Companion",
    submit_btn="▶️ Send",
)


app.launch()