shukdevdatta123 commited on
Commit
291f44e
·
verified ·
1 Parent(s): dcebc1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -36
app.py CHANGED
@@ -6,9 +6,6 @@ import os
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,29 +26,67 @@ def search_similar(query_embedding, index, stored_texts, top_k=3):
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")
@@ -112,18 +147,20 @@ if openai_api_key:
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
  )
 
6
  import numpy as np
7
  from sklearn.feature_extraction.text import TfidfVectorizer
8
  from sklearn.metrics.pairwise import cosine_similarity
 
 
 
9
 
10
  # Function to extract text from a PDF file
11
  def extract_text_from_pdf(pdf_file):
 
26
  results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
27
  return results
28
 
29
+ # Function to generate HTML with nice styling
30
+ def generate_html(response_content):
31
+ html_template = f"""
32
+ <!DOCTYPE html>
33
+ <html lang="en">
34
+ <head>
35
+ <meta charset="UTF-8">
36
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
37
+ <title>Course Query Response</title>
38
+ <style>
39
+ body {{
40
+ font-family: Arial, sans-serif;
41
+ margin: 0;
42
+ padding: 0;
43
+ background-color: #f4f4f9;
44
+ color: #333;
45
+ }}
46
+ .container {{
47
+ width: 80%;
48
+ margin: 30px auto;
49
+ background-color: white;
50
+ padding: 20px;
51
+ border-radius: 8px;
52
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
53
+ }}
54
+ h1 {{
55
+ color: #2C3E50;
56
+ font-size: 2em;
57
+ text-align: center;
58
+ }}
59
+ .response {{
60
+ background-color: #ecf0f1;
61
+ border-left: 5px solid #3498db;
62
+ padding: 20px;
63
+ font-size: 1.2em;
64
+ margin-top: 20px;
65
+ border-radius: 5px;
66
+ }}
67
+ footer {{
68
+ text-align: center;
69
+ margin-top: 30px;
70
+ font-size: 0.9em;
71
+ color: #7f8c8d;
72
+ }}
73
+ </style>
74
+ </head>
75
+ <body>
76
+ <div class="container">
77
+ <h1>Course Query Response</h1>
78
+ <div class="response">
79
+ <h3>Answer:</h3>
80
+ <p>{response_content}</p>
81
+ </div>
82
+ <footer>
83
+ <p>Generated by Course Query Assistant</p>
84
+ </footer>
85
+ </div>
86
+ </body>
87
+ </html>
88
+ """
89
+ return html_template
90
 
91
  # Streamlit app starts here
92
  st.title("Course Query Assistant")
 
147
  messages=[{"role": "user", "content": modified_prompt}]
148
  )
149
 
150
+ # Get the response content
151
+ response_content = response['choices'][0]['message']['content']
152
 
153
+ # Display the response in Streamlit
154
  st.write("### Intelligent Reply:")
155
+ st.write(response_content)
156
+
157
+ # Generate HTML content
158
+ html_content = generate_html(response_content)
159
 
160
+ # Provide the download button for the HTML file
 
161
  st.download_button(
162
+ label="Download Response as HTML",
163
+ data=html_content,
164
+ file_name="course_query_response.html",
165
+ mime="text/html"
166
  )