Spaces:
Running
Running
Update interim.py
Browse files- interim.py +21 -41
interim.py
CHANGED
@@ -85,9 +85,9 @@ class DocumentRAG:
|
|
85 |
)
|
86 |
documents = text_splitter.split_documents(documents)
|
87 |
|
88 |
-
# Combine text for summary
|
89 |
-
|
90 |
-
|
91 |
|
92 |
# Create embeddings and initialize retrieval chain
|
93 |
embeddings = OpenAIEmbeddings(api_key=self.api_key)
|
@@ -109,8 +109,8 @@ class DocumentRAG:
|
|
109 |
except Exception as e:
|
110 |
return f"Error processing documents: {str(e)}"
|
111 |
|
112 |
-
def generate_summary(self, text):
|
113 |
-
"""Generate a summary of the provided text."""
|
114 |
if not self.api_key:
|
115 |
return "API Key not set. Please set it in the environment variables."
|
116 |
try:
|
@@ -118,7 +118,7 @@ class DocumentRAG:
|
|
118 |
response = client.chat.completions.create(
|
119 |
model="gpt-4",
|
120 |
messages=[
|
121 |
-
{"role": "system", "content": "Summarize the document content concisely
|
122 |
{"role": "user", "content": text[:4000]}
|
123 |
],
|
124 |
temperature=0.3
|
@@ -127,8 +127,8 @@ class DocumentRAG:
|
|
127 |
except Exception as e:
|
128 |
return f"Error generating summary: {str(e)}"
|
129 |
|
130 |
-
def create_podcast(self):
|
131 |
-
"""Generate a podcast script and audio based on the
|
132 |
if not self.document_summary:
|
133 |
return "Please process documents before generating a podcast.", None
|
134 |
|
@@ -142,7 +142,7 @@ class DocumentRAG:
|
|
142 |
script_response = client.chat.completions.create(
|
143 |
model="gpt-4",
|
144 |
messages=[
|
145 |
-
{"role": "system", "content": "You are a professional podcast producer. Create a natural dialogue based on the provided document summary."},
|
146 |
{"role": "user", "content": f"""Based on the following document summary, create a 1-2 minute podcast script:
|
147 |
1. Clearly label the dialogue as 'Host 1:' and 'Host 2:'
|
148 |
2. Keep the content engaging and insightful.
|
@@ -201,31 +201,13 @@ class DocumentRAG:
|
|
201 |
except Exception as e:
|
202 |
return f"Error generating podcast: {str(e)}", None
|
203 |
|
204 |
-
def
|
205 |
-
"""
|
206 |
-
if not self.api_key:
|
207 |
-
return "API Key not set. Please set it in the environment variables."
|
208 |
-
try:
|
209 |
-
client = OpenAI(api_key=self.api_key)
|
210 |
-
response = client.chat.completions.create(
|
211 |
-
model="gpt-4",
|
212 |
-
messages=[
|
213 |
-
{"role": "system", "content": "Summarize the document content concisely and provide 3-5 key points for discussion."},
|
214 |
-
{"role": "user", "content": text[:4000]}
|
215 |
-
],
|
216 |
-
temperature=0.3
|
217 |
-
)
|
218 |
-
return response.choices[0].message.content
|
219 |
-
except Exception as e:
|
220 |
-
return f"Error generating summary: {str(e)}"
|
221 |
-
|
222 |
-
def handle_query(self, question, history):
|
223 |
-
"""Handle user queries."""
|
224 |
if not self.qa_chain:
|
225 |
return history + [("System", "Please process the documents first.")]
|
226 |
try:
|
227 |
preface = """
|
228 |
-
Instruction: Respond in
|
229 |
If you cannot provide an answer, say: "I am not sure about this question. Please try asking something else."
|
230 |
"""
|
231 |
query = f"{preface}\nQuery: {question}"
|
@@ -295,12 +277,11 @@ if st.button("Process Documents"):
|
|
295 |
else:
|
296 |
st.warning("No files uploaded.")
|
297 |
|
|
|
298 |
# Step 2: Generate Summaries
|
299 |
st.subheader("Step 2: Generate Summaries")
|
300 |
st.write("Select Summary Language:")
|
301 |
summary_language_options = ["English", "Hindi", "Spanish", "French", "German", "Chinese", "Japanese"]
|
302 |
-
cols = st.columns(len(summary_language_options))
|
303 |
-
# Default selected option
|
304 |
summary_language = st.radio(
|
305 |
"",
|
306 |
summary_language_options,
|
@@ -308,15 +289,14 @@ summary_language = st.radio(
|
|
308 |
key="summary_language"
|
309 |
)
|
310 |
|
311 |
-
if st.session_state.rag_system.document_summary:
|
312 |
-
st.text_area("Document Summary", st.session_state.rag_system.document_summary, height=200)
|
313 |
-
else:
|
314 |
-
st.info("Please process documents first to generate summaries.")
|
315 |
if st.button("Generate Summary"):
|
316 |
-
st.session_state.rag_system
|
317 |
-
st.session_state.rag_system.
|
318 |
-
|
319 |
-
|
|
|
|
|
|
|
320 |
|
321 |
# Step 3: Ask Questions
|
322 |
st.subheader("Step 3: Ask Questions")
|
@@ -362,4 +342,4 @@ if st.session_state.rag_system.document_summary:
|
|
362 |
else:
|
363 |
st.error(script)
|
364 |
else:
|
365 |
-
st.info("Please process documents and generate summaries before creating a podcast.")
|
|
|
85 |
)
|
86 |
documents = text_splitter.split_documents(documents)
|
87 |
|
88 |
+
# Combine text for later summary generation
|
89 |
+
self.document_text = " ".join([doc.page_content for doc in documents]) # Store for later use
|
90 |
+
|
91 |
|
92 |
# Create embeddings and initialize retrieval chain
|
93 |
embeddings = OpenAIEmbeddings(api_key=self.api_key)
|
|
|
109 |
except Exception as e:
|
110 |
return f"Error processing documents: {str(e)}"
|
111 |
|
112 |
+
def generate_summary(self, text, language):
|
113 |
+
"""Generate a summary of the provided text in the specified language."""
|
114 |
if not self.api_key:
|
115 |
return "API Key not set. Please set it in the environment variables."
|
116 |
try:
|
|
|
118 |
response = client.chat.completions.create(
|
119 |
model="gpt-4",
|
120 |
messages=[
|
121 |
+
{"role": "system", "content": f"Summarize the document content concisely in {language}. Provide 3-5 key points for discussion."},
|
122 |
{"role": "user", "content": text[:4000]}
|
123 |
],
|
124 |
temperature=0.3
|
|
|
127 |
except Exception as e:
|
128 |
return f"Error generating summary: {str(e)}"
|
129 |
|
130 |
+
def create_podcast(self, language):
|
131 |
+
"""Generate a podcast script and audio based on doc summary in the specified language."""
|
132 |
if not self.document_summary:
|
133 |
return "Please process documents before generating a podcast.", None
|
134 |
|
|
|
142 |
script_response = client.chat.completions.create(
|
143 |
model="gpt-4",
|
144 |
messages=[
|
145 |
+
{"role": "system", "content": f"You are a professional podcast producer. Create a natural dialogue in {language} based on the provided document summary."},
|
146 |
{"role": "user", "content": f"""Based on the following document summary, create a 1-2 minute podcast script:
|
147 |
1. Clearly label the dialogue as 'Host 1:' and 'Host 2:'
|
148 |
2. Keep the content engaging and insightful.
|
|
|
201 |
except Exception as e:
|
202 |
return f"Error generating podcast: {str(e)}", None
|
203 |
|
204 |
+
def handle_query(self, question, history, language):
|
205 |
+
"""Handle user queries in the specified language."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
if not self.qa_chain:
|
207 |
return history + [("System", "Please process the documents first.")]
|
208 |
try:
|
209 |
preface = """
|
210 |
+
Instruction: Respond in {language}. Be professional and concise, keeping the response under 300 words.
|
211 |
If you cannot provide an answer, say: "I am not sure about this question. Please try asking something else."
|
212 |
"""
|
213 |
query = f"{preface}\nQuery: {question}"
|
|
|
277 |
else:
|
278 |
st.warning("No files uploaded.")
|
279 |
|
280 |
+
|
281 |
# Step 2: Generate Summaries
|
282 |
st.subheader("Step 2: Generate Summaries")
|
283 |
st.write("Select Summary Language:")
|
284 |
summary_language_options = ["English", "Hindi", "Spanish", "French", "German", "Chinese", "Japanese"]
|
|
|
|
|
285 |
summary_language = st.radio(
|
286 |
"",
|
287 |
summary_language_options,
|
|
|
289 |
key="summary_language"
|
290 |
)
|
291 |
|
|
|
|
|
|
|
|
|
292 |
if st.button("Generate Summary"):
|
293 |
+
if hasattr(st.session_state.rag_system, "document_text") and st.session_state.rag_system.document_text:
|
294 |
+
summary = st.session_state.rag_system.generate_summary(st.session_state.rag_system.document_text, summary_language)
|
295 |
+
st.session_state.rag_system.document_summary = summary
|
296 |
+
st.text_area("Document Summary", summary, height=200)
|
297 |
+
else:
|
298 |
+
st.info("Please process documents first to generate summaries.")
|
299 |
+
|
300 |
|
301 |
# Step 3: Ask Questions
|
302 |
st.subheader("Step 3: Ask Questions")
|
|
|
342 |
else:
|
343 |
st.error(script)
|
344 |
else:
|
345 |
+
st.info("Please process documents and generate summaries before creating a podcast.")
|