artydev/Code_Assistant
0
1import streamlit as st2import os3import google.generativeai as genai4import time5 6os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")7genai.configure(api_key=os.environ["GOOGLE_API_KEY"])8 9icons = {"assistant": "robot.png", "user": "man-kddi.png"}10 11model = genai.GenerativeModel('gemini-1.5-flash-latest')12prompt = """You are a programming teaching assistant named GenXAI(Generative eXpert AI), created by Pachaiappan [linkdin](https://www.linkedin.com/in/pachaiappan) an AI Specialist. Answer only the programming, error-fixing and code-related question that being asked. 13Important note, If Question non-related to coding or programming means, you have to say: 'Please ask only coding-related questions.' except greeting and those kind of questions "who are you", "who created you".14previous_chat: 15{chat_history}16Human: {human_input}17Chatbot:"""18 19previous_response = ""20def get_response(query):21 global previous_response22 23 for i in st.session_state['history']:24 if i is not None:25 previous_response += f"Human: {i[0]}\n Chatbot: {i[1]}\n"26 27 response = model.generate_content(prompt.format(human_input=query, chat_history=previous_response))28 st.session_state['history'].append((query, response.text))29 return response.text30 31 32def response_streaming(text):33 for i in text:34 yield i35 time.sleep(0.001)36 37st.title("GenXAi")38st.caption("I am Generative EXpert Assistant for Programming Related Task!")39 40st.markdown("""41 <style>42 .justified-text {43 text-align: justify;44 }45 </style>46 """, unsafe_allow_html=True)47 48with st.sidebar:49 st.header("ABOUT:")50 51 st.caption("""52 <div class="justified-text">53 This is GenXai (Generation Expert AI), 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 GenXai to build and refine your questions, facilitating a more productive conversation.54 </div>55 """, unsafe_allow_html=True)56 57 for _ in range(17):58 st.write("") 59 st.subheader("Build By:")60 st.write("[Pachaiappan❤️](https://mr-vicky-01.github.io/Portfolio)")61 st.write("contact: [Email](mailto:pachaiappan1102@gamil.com)")62 63if 'messages' not in st.session_state:64 st.session_state.messages = [{'role': 'assistant', 'content': "I'm Here to help your programming realted questions😉"}]65 66if 'history' not in st.session_state:67 st.session_state.history = [] 68 69for message in st.session_state.messages:70 with st.chat_message(message['role'], avatar=icons[message['role']]):71 st.write(message['content'])72 73user_input = st.chat_input("Ask Your Questions 👉..")74if user_input:75 st.session_state.messages.append({'role': 'user', 'content': user_input})76 with st.chat_message("user", avatar="man-kddi.png"):77 st.write(user_input)78 79 with st.spinner("Thinking..."):80 response = get_response(user_input)81 82 with st.chat_message("user", avatar="robot.png"):83 st.write_stream(response_streaming(response))84 85 message = {"role": "assistant", "content": response}86 st.session_state.messages.append(message) 