Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 8 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def chatBot():
|
| 12 |
+
chatHistory = []
|
| 13 |
+
chatHistory.append(
|
| 14 |
+
{
|
| 15 |
+
"role": "user",
|
| 16 |
+
"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.",
|
| 17 |
+
}
|
| 18 |
+
)
|
| 19 |
+
startPrompt = model.generate_content(chatHistory)
|
| 20 |
+
chatHistory.append({"role": "model", "parts": [startPrompt.text]})
|
| 21 |
+
return chatHistory
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def response(text, history):
|
| 25 |
+
chatHistory = history
|
| 26 |
+
history = chatBot()
|
| 27 |
+
for human, assistant in chatHistory:
|
| 28 |
+
history.append({"role": "user", "parts": [human]})
|
| 29 |
+
history.append({"role": "model", "parts": [assistant]})
|
| 30 |
+
history.append({"role": "user", "parts": [text]})
|
| 31 |
+
|
| 32 |
+
response = model.generate_content(history)
|
| 33 |
+
history.append({"role": "model", "parts": [response.text]})
|
| 34 |
+
return response.text
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
app = gr.ChatInterface(
|
| 38 |
+
fn=response,
|
| 39 |
+
chatbot=gr.Chatbot(
|
| 40 |
+
show_copy_button="True",
|
| 41 |
+
),
|
| 42 |
+
theme=gr.themes.Soft(
|
| 43 |
+
primary_hue="sky",
|
| 44 |
+
secondary_hue="sky",
|
| 45 |
+
neutral_hue="stone",
|
| 46 |
+
font=[
|
| 47 |
+
gr.themes.GoogleFont("Montserrat"),
|
| 48 |
+
gr.themes.GoogleFont("Poppins"),
|
| 49 |
+
"system-ui",
|
| 50 |
+
"sans-serif",
|
| 51 |
+
],
|
| 52 |
+
font_mono=[
|
| 53 |
+
gr.themes.GoogleFont("Poppins"),
|
| 54 |
+
"ui-monospace",
|
| 55 |
+
"Consolas",
|
| 56 |
+
"monospace",
|
| 57 |
+
],
|
| 58 |
+
),
|
| 59 |
+
title="🤖 ByteBot: Your CS Companion",
|
| 60 |
+
submit_btn="▶️ Send",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
app.launch()
|