Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import osimport gradio as grimport google.generativeai as genaifrom 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()app.pyAdded+64