import streamlit as st import os import google.generativeai as genai import time os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY") genai.configure(api_key=os.environ["GOOGLE_API_KEY"]) icons = {"assistant": "chat.png", "user": "person.png"} model = genai.GenerativeModel('gemini-1.5-flash-latest') prompt = """You are a programming teaching assistant named CodeMate. Answer only the programming, error-fixing and code-related questions that are being asked. Important note: If the question is non-related to coding or programming, you have to say: 'Please ask only coding-related questions.' — except for greetings and questions like "Who are you?" or "Who created you?". previous_chat: {chat_history} Human: {human_input} Chatbot:""" previous_response = "" def get_response(query): global previous_response for i in st.session_state['history']: if i is not None: previous_response += f"Human: {i[0]}\n Chatbot: {i[1]}\n" response = model.generate_content(prompt.format(human_input=query, chat_history=previous_response)) st.session_state['history'].append((query, response.text)) return response.text def response_streaming(text): for i in text: yield i time.sleep(0.001) st.title("CodeMate") st.caption("I am Coding Assistant for Programming Related Task!") st.markdown(""" """, unsafe_allow_html=True) with st.sidebar: st.header("ABOUT:") st.caption("""
This is CodeMate, designed to assist with programming-related questions. This AI can help you answer your coding queries, fix errors, and much more. Additionally, you can chat with CodeMate to build and refine your questions, facilitating a more productive conversation.
""", unsafe_allow_html=True) if 'messages' not in st.session_state: st.session_state.messages = [{'role': 'assistant', 'content': "I'm Here to help your programming realted questions😉"}] if 'history' not in st.session_state: st.session_state.history = [] for message in st.session_state.messages: with st.chat_message(message['role'], avatar=icons[message['role']]): st.write(message['content']) user_input = st.chat_input("Ask Your Questions 👉..") if user_input: st.session_state.messages.append({'role': 'user', 'content': user_input}) with st.chat_message("user", avatar="person.png"): st.write(user_input) with st.spinner("Thinking..."): response = get_response(user_input) with st.chat_message("user", avatar="chat.png"): st.write_stream(response_streaming(response)) message = {"role": "assistant", "content": response} st.session_state.messages.append(message)