Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
##
|
2 |
import streamlit as st
|
3 |
import PyPDF2
|
4 |
import openai
|
@@ -7,6 +6,9 @@ import os
|
|
7 |
import numpy as np
|
8 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
9 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
|
10 |
|
11 |
# Function to extract text from a PDF file
|
12 |
def extract_text_from_pdf(pdf_file):
|
@@ -27,6 +29,30 @@ def search_similar(query_embedding, index, stored_texts, top_k=3):
|
|
27 |
results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
|
28 |
return results
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# Streamlit app starts here
|
31 |
st.title("Course Query Assistant")
|
32 |
|
@@ -86,6 +112,18 @@ if openai_api_key:
|
|
86 |
messages=[{"role": "user", "content": modified_prompt}]
|
87 |
)
|
88 |
|
|
|
|
|
|
|
89 |
# Display the response
|
90 |
st.write("### Intelligent Reply:")
|
91 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import PyPDF2
|
3 |
import openai
|
|
|
6 |
import numpy as np
|
7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
8 |
from sklearn.metrics.pairwise import cosine_similarity
|
9 |
+
from reportlab.lib.pagesizes import letter
|
10 |
+
from reportlab.pdfgen import canvas
|
11 |
+
from io import BytesIO
|
12 |
|
13 |
# Function to extract text from a PDF file
|
14 |
def extract_text_from_pdf(pdf_file):
|
|
|
29 |
results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
|
30 |
return results
|
31 |
|
32 |
+
# Function to create a PDF from the response
|
33 |
+
def create_pdf(response_text):
|
34 |
+
buffer = BytesIO()
|
35 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
36 |
+
width, height = letter
|
37 |
+
|
38 |
+
# Add the response text to the PDF
|
39 |
+
c.drawString(30, height - 30, "Intelligent Reply:")
|
40 |
+
text_object = c.beginText(30, height - 50)
|
41 |
+
text_object.setFont("Helvetica", 10)
|
42 |
+
text_object.setTextOrigin(30, height - 50)
|
43 |
+
|
44 |
+
# Add the response text, line by line
|
45 |
+
lines = response_text.split("\n")
|
46 |
+
for line in lines:
|
47 |
+
text_object.textLine(line)
|
48 |
+
|
49 |
+
c.drawText(text_object)
|
50 |
+
c.showPage()
|
51 |
+
c.save()
|
52 |
+
|
53 |
+
buffer.seek(0)
|
54 |
+
return buffer
|
55 |
+
|
56 |
# Streamlit app starts here
|
57 |
st.title("Course Query Assistant")
|
58 |
|
|
|
112 |
messages=[{"role": "user", "content": modified_prompt}]
|
113 |
)
|
114 |
|
115 |
+
# Extract the response text
|
116 |
+
response_text = response['choices'][0]['message']['content']
|
117 |
+
|
118 |
# Display the response
|
119 |
st.write("### Intelligent Reply:")
|
120 |
+
st.write(response_text)
|
121 |
+
|
122 |
+
# Button to download the response as PDF
|
123 |
+
pdf_buffer = create_pdf(response_text)
|
124 |
+
st.download_button(
|
125 |
+
label="Download Intelligent Reply as PDF",
|
126 |
+
data=pdf_buffer,
|
127 |
+
file_name="intelligent_reply.pdf",
|
128 |
+
mime="application/pdf"
|
129 |
+
)
|