Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
| 7 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 8 |
+
|
| 9 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 10 |
+
chat = model.start_chat(history=[])
|
| 11 |
+
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.
|
| 12 |
+
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""")
|
| 13 |
+
|
| 14 |
+
def get_response(query):
|
| 15 |
+
response = chat.send_message(query)
|
| 16 |
+
return response.text
|
| 17 |
+
|
| 18 |
+
def response_streaming(text):
|
| 19 |
+
for i in text:
|
| 20 |
+
yield i
|
| 21 |
+
time.sleep(0.001)
|
| 22 |
+
|
| 23 |
+
st.title("GenXAi")
|
| 24 |
+
st.text("I am Generative EXpert Assistant")
|
| 25 |
+
|
| 26 |
+
if 'messages' not in st.session_state:
|
| 27 |
+
st.session_state.messages = [{'role': 'assistant', 'content': "I'm Here to help your programming realted questions"}]
|
| 28 |
+
|
| 29 |
+
for message in st.session_state.messages:
|
| 30 |
+
with st.chat_message(message['role']):
|
| 31 |
+
st.write(message['content'])
|
| 32 |
+
|
| 33 |
+
user_input = st.chat_input("Ask Your Questions 👉..")
|
| 34 |
+
if user_input:
|
| 35 |
+
st.session_state.messages.append({'role': 'user', 'content': user_input})
|
| 36 |
+
with st.chat_message("user"):
|
| 37 |
+
st.write(user_input)
|
| 38 |
+
|
| 39 |
+
with st.spinner("Thinking..."):
|
| 40 |
+
response = get_response(user_input)
|
| 41 |
+
|
| 42 |
+
with st.chat_message("assistant"):
|
| 43 |
+
st.write(response)
|
| 44 |
+
# full_response = st.write_stream(get_response(response))
|
| 45 |
+
message = {"role": "assistant", "content": response}
|
| 46 |
+
st.session_state.messages.append(message)
|