engrphoenix commited on
Commit
48fb909
·
verified ·
1 Parent(s): 0ffe0e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  from PyPDF2 import PdfReader
3
  import pandas as pd
4
  from transformers import pipeline
 
5
 
6
  # Load the Hugging Face model for text generation (Bloom or any other open-source model)
7
  @st.cache_resource
@@ -29,12 +30,13 @@ def read_csv_file(file):
29
 
30
  # Function to search for a topic in the extracted content
31
  def search_topic_in_content(content, topic):
32
- topic_content = [line for line in content.split("\n") if topic.lower() in line.lower()]
33
- return "\n".join(topic_content)
 
34
 
35
- # Function to generate study material using Hugging Face model
36
- def generate_content(topic):
37
- prompt = f"Explain the topic '{topic}' in simple terms for electrical engineering students."
38
  response = text_generator(prompt, max_length=300, num_return_sequences=1)
39
  return response[0]['generated_text']
40
 
@@ -55,7 +57,7 @@ st.sidebar.header("AI-Based Tutor")
55
 
56
  # File upload section
57
  uploaded_file = st.sidebar.file_uploader("Upload Study Material (PDF/TXT/CSV)", type=["pdf", "txt", "csv"])
58
- topic = st.sidebar.text_input("Enter a topic (e.g., DC Motors, Transformers)")
59
 
60
  # Process uploaded file
61
  content = ""
@@ -77,14 +79,16 @@ if uploaded_file:
77
  if st.button("Generate Study Material"):
78
  if topic:
79
  st.header(f"Study Material: {topic}")
 
80
  filtered_content = search_topic_in_content(content, topic) if content else ""
81
  if filtered_content:
82
- st.write("**Relevant Extracted Content:**")
83
  st.write(filtered_content)
84
-
85
- ai_content = generate_content(topic)
86
- st.write("**AI-Generated Content:**")
87
- st.write(ai_content)
 
88
  else:
89
  st.warning("Please enter a topic!")
90
 
 
2
  from PyPDF2 import PdfReader
3
  import pandas as pd
4
  from transformers import pipeline
5
+ import random
6
 
7
  # Load the Hugging Face model for text generation (Bloom or any other open-source model)
8
  @st.cache_resource
 
30
 
31
  # Function to search for a topic in the extracted content
32
  def search_topic_in_content(content, topic):
33
+ sentences = content.split(".") # Break content into sentences
34
+ topic_sentences = [s for s in sentences if topic.lower() in s.lower()] # Filter sentences containing the topic
35
+ return ". ".join(topic_sentences) if topic_sentences else None
36
 
37
+ # Function to generate content using Hugging Face model
38
+ def generate_ai_content(topic):
39
+ prompt = f"Explain '{topic}' in simple terms for engineering students. Provide examples and applications."
40
  response = text_generator(prompt, max_length=300, num_return_sequences=1)
41
  return response[0]['generated_text']
42
 
 
57
 
58
  # File upload section
59
  uploaded_file = st.sidebar.file_uploader("Upload Study Material (PDF/TXT/CSV)", type=["pdf", "txt", "csv"])
60
+ topic = st.sidebar.text_input("Enter a topic (e.g., Newton's Third Law, DC Motors)")
61
 
62
  # Process uploaded file
63
  content = ""
 
79
  if st.button("Generate Study Material"):
80
  if topic:
81
  st.header(f"Study Material: {topic}")
82
+ # Extract relevant content from the uploaded material
83
  filtered_content = search_topic_in_content(content, topic) if content else ""
84
  if filtered_content:
85
+ st.write("**Relevant Extracted Content from Uploaded Material:**")
86
  st.write(filtered_content)
87
+ else:
88
+ st.warning("No relevant content found in the uploaded material. Generating AI-based content instead.")
89
+ ai_content = generate_ai_content(topic)
90
+ st.write("**AI-Generated Content:**")
91
+ st.write(ai_content)
92
  else:
93
  st.warning("Please enter a topic!")
94