shukdevdatta123 commited on
Commit
0bad8e4
·
verified ·
1 Parent(s): 6e449bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -1
app.py CHANGED
@@ -6,6 +6,8 @@ import os
6
  import numpy as np
7
  from sklearn.feature_extraction.text import TfidfVectorizer
8
  from sklearn.metrics.pairwise import cosine_similarity
 
 
9
 
10
  # Function to extract text from a PDF file
11
  def extract_text_from_pdf(pdf_file):
@@ -39,6 +41,15 @@ def save_code_to_file(code, filename="generated_code.txt"):
39
  with open(filename, "w") as f:
40
  f.write(code)
41
 
 
 
 
 
 
 
 
 
 
42
  # Streamlit app starts here
43
  st.title("AI Assistance")
44
 
@@ -50,7 +61,7 @@ if openai_api_key:
50
 
51
  # Sidebar to toggle between Course Query Assistant and Code Generator
52
  st.sidebar.title("Select Mode")
53
- mode = st.sidebar.radio("Choose an option", ("Course Query Assistant", "Code Generator"))
54
 
55
  if mode == "Course Query Assistant":
56
  st.header("Course Query Assistant")
@@ -147,3 +158,47 @@ if openai_api_key:
147
  )
148
  else:
149
  st.error("Please provide a prompt to generate the code.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import numpy as np
7
  from sklearn.feature_extraction.text import TfidfVectorizer
8
  from sklearn.metrics.pairwise import cosine_similarity
9
+ import speech_recognition as sr
10
+ from io import StringIO
11
 
12
  # Function to extract text from a PDF file
13
  def extract_text_from_pdf(pdf_file):
 
41
  with open(filename, "w") as f:
42
  f.write(code)
43
 
44
+ # Function to generate AI-based study notes and summaries
45
+ def generate_summary(text):
46
+ prompt = f"Summarize the following text into key points:\n\n{text}"
47
+ response = openai.ChatCompletion.create(
48
+ model="gpt-4o-mini",
49
+ messages=[{"role": "user", "content": prompt}]
50
+ )
51
+ return response['choices'][0]['message']['content']
52
+
53
  # Streamlit app starts here
54
  st.title("AI Assistance")
55
 
 
61
 
62
  # Sidebar to toggle between Course Query Assistant and Code Generator
63
  st.sidebar.title("Select Mode")
64
+ mode = st.sidebar.radio("Choose an option", ("Course Query Assistant", "Code Generator", "AI Chatbot Tutor", "AI Study Notes & Summaries"))
65
 
66
  if mode == "Course Query Assistant":
67
  st.header("Course Query Assistant")
 
158
  )
159
  else:
160
  st.error("Please provide a prompt to generate the code.")
161
+
162
+ elif mode == "AI Chatbot Tutor":
163
+ st.header("AI Chatbot Tutor")
164
+
165
+ # Chat interface for the AI tutor
166
+ chat_history = []
167
+
168
+ def chat_with_bot(query):
169
+ chat_history.append({"role": "user", "content": query})
170
+ response = openai.ChatCompletion.create(
171
+ model="gpt-4o-mini",
172
+ messages=chat_history
173
+ )
174
+ chat_history.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
175
+ return response['choices'][0]['message']['content']
176
+
177
+ user_query = st.text_input("Ask a question:")
178
+
179
+ if user_query:
180
+ with st.spinner("Getting answer..."):
181
+ bot_response = chat_with_bot(user_query)
182
+ st.write(f"### AI Response: {bot_response}")
183
+
184
+ elif mode == "AI Study Notes & Summaries":
185
+ st.header("AI Study Notes & Summaries")
186
+
187
+ # Upload course materials for summarization
188
+ uploaded_files_for_summary = st.file_uploader("Upload Course Materials (PDFs) for Summarization", type=["pdf"], accept_multiple_files=True)
189
+
190
+ if uploaded_files_for_summary:
191
+ st.write("Generating study notes and summaries...")
192
+
193
+ # Extract text from PDFs
194
+ all_text = ""
195
+ for uploaded_file in uploaded_files_for_summary:
196
+ text = extract_text_from_pdf(uploaded_file)
197
+ all_text += text
198
+
199
+ # Generate summary using AI
200
+ summary = generate_summary(all_text)
201
+
202
+ # Display the summary
203
+ st.write("### AI-Generated Summary:")
204
+ st.write(summary)