edithram23 commited on
Commit
b90926c
·
verified ·
1 Parent(s): 5890dda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -68
app.py CHANGED
@@ -1,68 +1,66 @@
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
- from dotenv import load_dotenv
7
- load_dotenv()
8
- genai.configure(api_key=os.getenv('GEMINI'))
9
- database_str=''
10
- with open('database.txt', 'r',encoding='utf-8') as f:
11
- database_str = f.read()
12
-
13
-
14
- def generate_response(query):
15
- prompt = f'''
16
- You are a Course suggestor based on the user requirement and the from the given database which consist of
17
- the course name and description of the course.
18
-
19
- You're tasked to use the description of each course and compare it with the user input and output which course's
20
- description matches the user requirement.
21
- Output the course name alone which matches the user requirement.
22
- 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.
23
- # Database
24
- {database_str}
25
-
26
- # User Input
27
- {query}
28
-
29
- # Output : (Course Name with | as splitter)
30
- '''
31
- model = genai.GenerativeModel("gemini-1.5-flash")
32
- response = model.generate_content(prompt)
33
-
34
- return response.text.split("|")
35
-
36
-
37
- # Define session state variables
38
- if 'messages' not in st.session_state:
39
- st.session_state.messages = []
40
- if 'mess' not in st.session_state:
41
- st.session_state.mess=[]
42
-
43
-
44
- if st.sidebar.button("RESET"):
45
- st.session_state.messages=[]
46
- st.session_state.mess=[]
47
-
48
- # User input
49
- st.title('Analytics Vidhya Course Finder')
50
- user_input = st.chat_input('Write your message here...')
51
-
52
- if user_input:
53
- # Append user input to messages
54
- st.session_state.messages.append({"role": "user", "content": user_input})
55
- st.session_state.mess+=[user_input]
56
- # Generate chatbot response
57
- bot_response = generate_response(st.session_state.mess)
58
- st.session_state.messages.append({"role": "bot", "content": bot_response})
59
-
60
- # Display chat messages in correct order
61
- for message in st.session_state.messages:
62
- if message["role"] == "user":
63
- with st.chat_message("human"):
64
- st.write(message['content'])
65
- else:
66
- with st.chat_message("ai"):
67
- for i in message['content']:
68
- st.write('* '+i)
 
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
+ genai.configure(api_key=os.getenv('GEMINI'))
7
+ database_str=''
8
+ with open('database.txt', 'r',encoding='utf-8') as f:
9
+ database_str = f.read()
10
+
11
+
12
+ def generate_response(query):
13
+ prompt = f'''
14
+ You are a Course suggestor based on the user requirement and the from the given database which consist of
15
+ the course name and description of the course.
16
+
17
+ You're tasked to use the description of each course and compare it with the user input and output which course's
18
+ description matches the user requirement.
19
+ Output the course name alone which matches the user requirement.
20
+ 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.
21
+ # Database
22
+ {database_str}
23
+
24
+ # User Input
25
+ {query}
26
+
27
+ # Output : (Course Name with | as splitter)
28
+ '''
29
+ model = genai.GenerativeModel("gemini-1.5-flash")
30
+ response = model.generate_content(prompt)
31
+
32
+ return response.text.split("|")
33
+
34
+
35
+ # Define session state variables
36
+ if 'messages' not in st.session_state:
37
+ st.session_state.messages = []
38
+ if 'mess' not in st.session_state:
39
+ st.session_state.mess=[]
40
+
41
+
42
+ if st.sidebar.button("RESET"):
43
+ st.session_state.messages=[]
44
+ st.session_state.mess=[]
45
+
46
+ # User input
47
+ st.title('Analytics Vidhya Course Finder')
48
+ user_input = st.chat_input('Write your message here...')
49
+
50
+ if user_input:
51
+ # Append user input to messages
52
+ st.session_state.messages.append({"role": "user", "content": user_input})
53
+ st.session_state.mess+=[user_input]
54
+ # Generate chatbot response
55
+ bot_response = generate_response(st.session_state.mess)
56
+ st.session_state.messages.append({"role": "bot", "content": bot_response})
57
+
58
+ # Display chat messages in correct order
59
+ for message in st.session_state.messages:
60
+ if message["role"] == "user":
61
+ with st.chat_message("human"):
62
+ st.write(message['content'])
63
+ else:
64
+ with st.chat_message("ai"):
65
+ for i in message['content']:
66
+ st.write('* '+i)