Skip to content

Commit

Permalink
feat: added initial setup of project with working databasepdf
Browse files Browse the repository at this point in the history
  • Loading branch information
Dospalko committed Nov 25, 2023
1 parent c459166 commit d810033
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
from flask_migrate import Migrate
from PyPDF2 import PdfReader
import re
import openai
from dotenv import load_dotenv
load_dotenv()


# Configure OpenAI with your API Key
openai.api_key = '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',

app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:heslo@localhost/hackathon'
Expand All @@ -23,6 +28,13 @@ def __init__(self, text, filename):
self.text = text
self.filename = filename

class PdfSummary(db.Model):
id = db.Column(db.Integer, primary_key=True)
summary_text = db.Column(db.Text)
pdf_text_id = db.Column(db.Integer, db.ForeignKey('pdf_text.id'), nullable=False)

pdf_text = db.relationship('PdfText', backref=db.backref('summary', lazy=True))

@app.route('/upload_pdf', methods=['POST'])
def upload_pdf():
uploaded_file = request.files.get('file')
Expand Down Expand Up @@ -57,6 +69,37 @@ def delete_pdf_text(pdf_text_id):
else:
return jsonify({"message": "PDF Text not found"}), 404


@app.route('/summarize_pdf', methods=['POST'])
def summarize_pdf():
data = request.json
pdf_text_id = data.get('pdf_text_id')

if not pdf_text_id:
return jsonify({"error": "PDF text ID is required"}), 400

pdf_text_record = PdfText.query.get(pdf_text_id)
if not pdf_text_record:
return jsonify({"error": "PDF text not found"}), 404

try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Summarize the following text:\n\n{pdf_text_record.text}",
max_tokens=150
)
summary = response.choices[0].text.strip()

# Save the summary to the database
new_summary = PdfSummary(summary_text=summary, pdf_text_id=pdf_text_id)
db.session.add(new_summary)
db.session.commit()

return jsonify({"summary": summary})
except Exception as e:
print(f"Error: {e}")
return jsonify({"error": "An error occurred during summarization"}), 500

def init_db():
db.create_all()

Expand Down
34 changes: 34 additions & 0 deletions backend/migrations/versions/08afc1f4ebcc_summarize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""summarize
Revision ID: 08afc1f4ebcc
Revises: c4b843d1ef4c
Create Date: 2023-11-25 14:41:14.451331
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '08afc1f4ebcc'
down_revision = 'c4b843d1ef4c'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('pdf_summary',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('summary_text', sa.Text(), nullable=True),
sa.Column('pdf_text_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['pdf_text_id'], ['pdf_text.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('pdf_summary')
# ### end Alembic commands ###
11 changes: 11 additions & 0 deletions backend/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import openai

# Set the API key directly
openai.api_key = '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'

response = openai.Completion.create(
engine="gpt-3.5-turbo-instruct",
prompt="Write a tagline for an ice cream shop."
)

print(response.choices[0].text.strip())
25 changes: 23 additions & 2 deletions frontend/src/components/PdfUpload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const PdfUpload = () => {
const [pdfTexts, setPdfTexts] = useState([]);
const [selectedFile, setSelectedFile] = useState(null);
const [isUploading, setIsUploading] = useState(false);
const [selectedPdfTextId, setSelectedPdfTextId] = useState(null);
const [summary, setSummary] = useState('');

const fetchPdfTexts = async () => {
try {
Expand Down Expand Up @@ -44,12 +46,23 @@ const PdfUpload = () => {
}
};

const summarizePdfText = async (pdfTextId) => {
setSelectedPdfTextId(pdfTextId);
try {
const response = await axios.post('/summarize_pdf', { pdf_text_id: pdfTextId });
setSummary(response.data.summary); // Store the summary in state
} catch (error) {
console.error('Failed to summarize PDF text:', error);
setSummary(''); // Clear previous summary on error
}
};

useEffect(() => {
fetchPdfTexts();
}, []);

return (
<div className="p-10 m-auto text-white">
<div className="p-10 m-auto text-black">
<input
type="file"
accept=".pdf"
Expand All @@ -67,11 +80,19 @@ const PdfUpload = () => {
<ul>
{pdfTexts.map((text) => (
<li key={text.id}>

{text.filename}
<button onClick={() => summarizePdfText(text.id)}>
Summarize
</button>
<button onClick={() => deletePdfText(text.id)}>
Delete
</button>
{selectedPdfTextId === text.id && summary && (
<div>
<p>Summary:</p>
<p>{summary}</p>
</div>
)}
</li>
))}
</ul>
Expand Down

0 comments on commit d810033

Please sign in to comment.