Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from huggify_data.scrape_modules import PDFQnAGenerator
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
def main():
|
| 7 |
+
st.title("PDF Question-Answer Generator")
|
| 8 |
+
|
| 9 |
+
# Sidebar for uploading the PDF file
|
| 10 |
+
st.sidebar.title("Upload PDF")
|
| 11 |
+
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type="pdf")
|
| 12 |
+
|
| 13 |
+
# Text input for OpenAI API key
|
| 14 |
+
openai_api_key = st.sidebar.text_input("Enter your OpenAI API key", type="password")
|
| 15 |
+
|
| 16 |
+
if uploaded_file is not None and openai_api_key:
|
| 17 |
+
# Save the uploaded PDF to a temporary file
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
| 19 |
+
temp_pdf.write(uploaded_file.read())
|
| 20 |
+
temp_pdf_path = temp_pdf.name
|
| 21 |
+
|
| 22 |
+
# Process the PDF and generate the questions and answers
|
| 23 |
+
generator = PDFQnAGenerator(temp_pdf_path, openai_api_key)
|
| 24 |
+
generator.process_scraped_content()
|
| 25 |
+
generator.generate_questions_answers()
|
| 26 |
+
df = generator.convert_to_dataframe()
|
| 27 |
+
|
| 28 |
+
# Display the resulting DataFrame
|
| 29 |
+
st.subheader("Generated Question-Answer Pairs")
|
| 30 |
+
st.write(df)
|
| 31 |
+
|
| 32 |
+
# Option to download the DataFrame as a CSV
|
| 33 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 34 |
+
st.download_button(
|
| 35 |
+
label="Download as CSV",
|
| 36 |
+
data=csv,
|
| 37 |
+
file_name='questions_answers.csv',
|
| 38 |
+
mime='text/csv',
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
main()
|