Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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
|
| 8 |
+
def load_text_generator():
|
| 9 |
+
return pipeline("text-generation", model="bigscience/bloom-560m") # Smaller version of Bloom for free use
|
| 10 |
+
|
| 11 |
+
text_generator = load_text_generator()
|
| 12 |
+
|
| 13 |
+
# Function to extract text from a PDF file
|
| 14 |
+
def extract_pdf_content(pdf_file):
|
| 15 |
+
reader = PdfReader(pdf_file)
|
| 16 |
+
content = ""
|
| 17 |
+
for page in reader.pages:
|
| 18 |
+
content += page.extract_text()
|
| 19 |
+
return content
|
| 20 |
+
|
| 21 |
+
# Function to extract content from a text file
|
| 22 |
+
def extract_text_file(file):
|
| 23 |
+
return file.read().decode("utf-8")
|
| 24 |
+
|
| 25 |
+
# Function to load a CSV file
|
| 26 |
+
def read_csv_file(file):
|
| 27 |
+
df = pd.read_csv(file)
|
| 28 |
+
return df.to_string()
|
| 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 |
+
|
| 41 |
+
# Function to generate a quiz question
|
| 42 |
+
def generate_quiz(topic):
|
| 43 |
+
questions = [
|
| 44 |
+
f"What is the fundamental principle of {topic}?",
|
| 45 |
+
f"Name a practical application of {topic}.",
|
| 46 |
+
f"What are the key equations associated with {topic}?",
|
| 47 |
+
f"Describe how {topic} is used in real-world scenarios.",
|
| 48 |
+
f"List common problems and solutions related to {topic}.",
|
| 49 |
+
]
|
| 50 |
+
return random.choice(questions)
|
| 51 |
+
|
| 52 |
+
# Streamlit App
|
| 53 |
+
st.title("Generative AI for Electrical Engineering Education")
|
| 54 |
+
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 = ""
|
| 62 |
+
if uploaded_file:
|
| 63 |
+
file_type = uploaded_file.name.split(".")[-1]
|
| 64 |
+
|
| 65 |
+
if file_type == "pdf":
|
| 66 |
+
content = extract_pdf_content(uploaded_file)
|
| 67 |
+
elif file_type == "txt":
|
| 68 |
+
content = extract_text_file(uploaded_file)
|
| 69 |
+
elif file_type == "csv":
|
| 70 |
+
content = read_csv_file(uploaded_file)
|
| 71 |
+
|
| 72 |
+
st.sidebar.success(f"{uploaded_file.name} uploaded successfully!")
|
| 73 |
+
st.write("**Extracted Content from File:**")
|
| 74 |
+
st.write(content[:1000] + "...") # Display a snippet of the content
|
| 75 |
+
|
| 76 |
+
# Generate study material
|
| 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 |
+
|
| 91 |
+
# Generate quiz
|
| 92 |
+
if st.button("Generate Quiz"):
|
| 93 |
+
if topic:
|
| 94 |
+
st.header("Quiz Question")
|
| 95 |
+
question = generate_quiz(topic)
|
| 96 |
+
st.write(question)
|
| 97 |
+
else:
|
| 98 |
+
st.warning("Please enter a topic!")
|