Rename gemini_app.py to app.py
Browse files- gemini_app.py → app.py +71 -72
gemini_app.py → app.py
RENAMED
|
@@ -1,72 +1,71 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import google.generativeai as genai
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import os
|
| 5 |
-
import numpy as np
|
| 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 |
-
response
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
st.session_state.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
st.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
st.session_state.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
genai.configure(api_key=os.getenv('GEMINI'))
|
| 8 |
+
database_str=''
|
| 9 |
+
with open('data_base.txt', 'r',encoding='utf-8') as f:
|
| 10 |
+
database_str = f.read()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate_response(query):
|
| 14 |
+
prompt = f'''
|
| 15 |
+
You are a Course suggestor based on the user requirement and the from the given database which consist of
|
| 16 |
+
the course name and description of the course.
|
| 17 |
+
|
| 18 |
+
You're tasked to use the description of each course and compare it with the user input and output which course's
|
| 19 |
+
description matches the user requirement.
|
| 20 |
+
Output the course name & Course Link alone which matches the user requirement.
|
| 21 |
+
you may output a max of 3 courses if you find that are good matches. name of the course should be exactly same as the database provided to you along with its link provided
|
| 22 |
+
|
| 23 |
+
# Database
|
| 24 |
+
{database_str}
|
| 25 |
+
|
| 26 |
+
# User Input
|
| 27 |
+
{query}
|
| 28 |
+
|
| 29 |
+
# Output : Course Name||Coure LINK \ Course Name||Course LINK \....
|
| 30 |
+
'''
|
| 31 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 32 |
+
response = model.generate_content(prompt)
|
| 33 |
+
return response.text.split("\\")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Define session state variables
|
| 37 |
+
if 'messages' not in st.session_state:
|
| 38 |
+
st.session_state.messages = []
|
| 39 |
+
if 'mess' not in st.session_state:
|
| 40 |
+
st.session_state.mess=[]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
if st.sidebar.button("RESET"):
|
| 44 |
+
st.session_state.messages=[]
|
| 45 |
+
st.session_state.mess=[]
|
| 46 |
+
|
| 47 |
+
# User input
|
| 48 |
+
st.title('Analytics Vidhya Course Finder')
|
| 49 |
+
user_input = st.chat_input('Write your message here...')
|
| 50 |
+
|
| 51 |
+
if user_input:
|
| 52 |
+
# Append user input to messages
|
| 53 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 54 |
+
st.session_state.mess+=[user_input]
|
| 55 |
+
# Generate chatbot response
|
| 56 |
+
bot_response = generate_response(st.session_state.mess)
|
| 57 |
+
st.session_state.messages.append({"role": "bot", "content": bot_response})
|
| 58 |
+
|
| 59 |
+
# Display chat messages in correct order
|
| 60 |
+
for message in st.session_state.messages:
|
| 61 |
+
if message["role"] == "user":
|
| 62 |
+
with st.chat_message("human"):
|
| 63 |
+
st.write(message['content'])
|
| 64 |
+
else:
|
| 65 |
+
with st.chat_message("ai"):
|
| 66 |
+
for i in message['content']:
|
| 67 |
+
name = i.split('||')[0]
|
| 68 |
+
link = i.split("||")[1]
|
| 69 |
+
st.markdown(f"[{name}]({link})", unsafe_allow_html=True)
|
| 70 |
+
|
| 71 |
+
|
|
|