shukdevdatta123 commited on
Commit
4287e6f
·
verified ·
1 Parent(s): fc93c4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -72
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 HTML with nice styling
31
- def generate_html(response_content):
32
- # Escape any HTML special characters to prevent unwanted rendering issues
33
- safe_content = html.escape(response_content)
34
 
35
- html_template = f"""
36
- <!DOCTYPE html>
37
- <html lang="en">
38
- <head>
39
- <meta charset="UTF-8">
40
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
41
- <title>Course Query Response</title>
42
- <style>
43
- body {{
44
- font-family: Arial, sans-serif;
45
- margin: 0;
46
- padding: 0;
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-3.5-turbo response
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 HTML content based on the exact response content
162
- html_content = generate_html(response_content)
163
 
164
- # Provide the download button for the HTML file
165
  st.download_button(
166
- label="Download Response as HTML",
167
- data=html_content,
168
- file_name="course_query_response.html",
169
- mime="text/html"
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
  )