Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import numpy as np
|
|
7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
8 |
from sklearn.metrics.pairwise import cosine_similarity
|
9 |
import html
|
|
|
10 |
|
11 |
# Function to extract text from a PDF file
|
12 |
def extract_text_from_pdf(pdf_file):
|
@@ -27,70 +28,22 @@ 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 |
-
# Function to generate
|
31 |
-
def
|
32 |
-
|
33 |
-
safe_content = html.escape(response_content)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
background-color: #f4f4f9;
|
48 |
-
color: #333;
|
49 |
-
}}
|
50 |
-
.container {{
|
51 |
-
width: 80%;
|
52 |
-
margin: 30px auto;
|
53 |
-
background-color: white;
|
54 |
-
padding: 20px;
|
55 |
-
border-radius: 8px;
|
56 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
57 |
-
}}
|
58 |
-
h1 {{
|
59 |
-
color: #2C3E50;
|
60 |
-
font-size: 2em;
|
61 |
-
text-align: center;
|
62 |
-
}}
|
63 |
-
.response {{
|
64 |
-
background-color: #ecf0f1;
|
65 |
-
border-left: 5px solid #3498db;
|
66 |
-
padding: 20px;
|
67 |
-
font-size: 1.2em;
|
68 |
-
margin-top: 20px;
|
69 |
-
border-radius: 5px;
|
70 |
-
}}
|
71 |
-
footer {{
|
72 |
-
text-align: center;
|
73 |
-
margin-top: 30px;
|
74 |
-
font-size: 0.9em;
|
75 |
-
color: #7f8c8d;
|
76 |
-
}}
|
77 |
-
</style>
|
78 |
-
</head>
|
79 |
-
<body>
|
80 |
-
<div class="container">
|
81 |
-
<h1>Course Query Response</h1>
|
82 |
-
<div class="response">
|
83 |
-
<h3>Answer:</h3>
|
84 |
-
<p>{safe_content}</p>
|
85 |
-
</div>
|
86 |
-
<footer>
|
87 |
-
<p>Generated by Course Query Assistant</p>
|
88 |
-
</footer>
|
89 |
-
</div>
|
90 |
-
</body>
|
91 |
-
</html>
|
92 |
-
"""
|
93 |
-
return html_template
|
94 |
|
95 |
# Streamlit app starts here
|
96 |
st.title("Course Query Assistant")
|
@@ -145,9 +98,9 @@ if openai_api_key:
|
|
145 |
context = "\n".join([result[0] for result in results])
|
146 |
modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."
|
147 |
|
148 |
-
# Get the GPT-
|
149 |
response = openai.ChatCompletion.create(
|
150 |
-
model="gpt-4o-mini",
|
151 |
messages=[{"role": "user", "content": modified_prompt}]
|
152 |
)
|
153 |
|
@@ -158,13 +111,13 @@ if openai_api_key:
|
|
158 |
st.write("### Intelligent Reply:")
|
159 |
st.write(response_content)
|
160 |
|
161 |
-
# Generate
|
162 |
-
|
163 |
|
164 |
-
# Provide the download button for the
|
165 |
st.download_button(
|
166 |
-
label="Download Response as
|
167 |
-
data=
|
168 |
-
file_name="course_query_response.
|
169 |
-
mime="
|
170 |
)
|
|
|
7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
8 |
from sklearn.metrics.pairwise import cosine_similarity
|
9 |
import html
|
10 |
+
from docx import Document # New import for working with docx files
|
11 |
|
12 |
# Function to extract text from a PDF file
|
13 |
def extract_text_from_pdf(pdf_file):
|
|
|
28 |
results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
|
29 |
return results
|
30 |
|
31 |
+
# Function to generate a docx file with the response content
|
32 |
+
def generate_docx(response_content):
|
33 |
+
doc = Document()
|
|
|
34 |
|
35 |
+
# Adding title and response to the docx
|
36 |
+
doc.add_heading('Course Query Response', 0)
|
37 |
+
doc.add_heading('Answer:', level=1)
|
38 |
+
doc.add_paragraph(response_content)
|
39 |
+
|
40 |
+
# Save the document to a byte stream to allow for download in Streamlit
|
41 |
+
from io import BytesIO
|
42 |
+
doc_io = BytesIO()
|
43 |
+
doc.save(doc_io)
|
44 |
+
doc_io.seek(0)
|
45 |
+
|
46 |
+
return doc_io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Streamlit app starts here
|
49 |
st.title("Course Query Assistant")
|
|
|
98 |
context = "\n".join([result[0] for result in results])
|
99 |
modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."
|
100 |
|
101 |
+
# Get the GPT-4 response
|
102 |
response = openai.ChatCompletion.create(
|
103 |
+
model="gpt-4o-mini", # Update to GPT-4 (or your desired model)
|
104 |
messages=[{"role": "user", "content": modified_prompt}]
|
105 |
)
|
106 |
|
|
|
111 |
st.write("### Intelligent Reply:")
|
112 |
st.write(response_content)
|
113 |
|
114 |
+
# Generate a docx file based on the exact response content
|
115 |
+
docx_file = generate_docx(response_content)
|
116 |
|
117 |
+
# Provide the download button for the .docx file
|
118 |
st.download_button(
|
119 |
+
label="Download Response as DOCX",
|
120 |
+
data=docx_file,
|
121 |
+
file_name="course_query_response.docx",
|
122 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
123 |
)
|