File size: 1,796 Bytes
250bef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"])

model = genai.GenerativeModel('gemini-1.5-flash-latest')
chat = model.start_chat(history=[])
chat.send_message("""You are a programming teaching assistant named GenXAI(Generative eXpert AI), created by Pachaiappan. Answer only the programming, error-fixing and code-related question that being asked. 
Important note, If Question non-related to coding or programming means, you have to say: 'Please ask only coding-related questions.' except those kind of questions "who are you", "who created you""")

def get_response(query):
    response = chat.send_message(query)
    return response.text

def response_streaming(text):
    for i in text:
        yield i
        time.sleep(0.001)

st.title("GenXAi")
st.text("I am Generative EXpert Assistant")

if 'messages' not in st.session_state:
    st.session_state.messages = [{'role': 'assistant', 'content': "I'm Here to help your programming realted questions"}]
    
for message in st.session_state.messages:
    with st.chat_message(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"):
        st.write(user_input)
        
    with st.spinner("Thinking..."):
        response = get_response(user_input)
        
    with st.chat_message("assistant"):
        st.write(response)
        # full_response = st.write_stream(get_response(response))
        message = {"role": "assistant", "content": response}
        st.session_state.messages.append(message)