Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
@@ -16,8 +16,8 @@ import torch
|
|
16 |
import random
|
17 |
|
18 |
class DialogueItem(BaseModel):
|
19 |
-
speaker: Literal["Jane", "John"]
|
20 |
-
display_speaker: str = "Jane"
|
21 |
text: str
|
22 |
|
23 |
class Dialogue(BaseModel):
|
@@ -96,9 +96,9 @@ def query_llm_for_additional_info(topic: str, existing_text: str) -> str:
|
|
96 |
print(additional_info)
|
97 |
return additional_info
|
98 |
|
99 |
-
##
|
100 |
def research_topic(topic: str) -> str:
|
101 |
-
print("[LOG] Researching topic using Tavily API and
|
102 |
tavily_api_key = os.environ.get("TAVILY_API_KEY")
|
103 |
if not tavily_api_key:
|
104 |
print("[ERROR] TAVILY_API_KEY not found in environment variables.")
|
@@ -164,30 +164,36 @@ def research_topic(topic: str) -> str:
|
|
164 |
"Make sure to include all relevant and updated information."
|
165 |
)
|
166 |
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
"Content-Type": "application/json"
|
170 |
}
|
171 |
-
|
172 |
-
"model": "
|
173 |
"messages": [{"role": "user", "content": research_prompt}],
|
174 |
"max_tokens": 4096,
|
175 |
"temperature": 0.6
|
176 |
}
|
177 |
try:
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
response_json =
|
182 |
report_content = response_json["choices"][0]["message"]["content"].strip()
|
183 |
-
print("[LOG] Research report generated successfully.")
|
184 |
except Exception as e:
|
185 |
-
print("[ERROR]
|
186 |
report_content = f"Error generating research report: {str(e)}"
|
187 |
|
188 |
return report_content
|
189 |
|
190 |
-
##
|
191 |
def generate_pdf_report(report_text: str) -> str:
|
192 |
"""
|
193 |
Generate a PDF file from the given report text using wkhtmltopdf.
|
@@ -195,8 +201,8 @@ def generate_pdf_report(report_text: str) -> str:
|
|
195 |
"""
|
196 |
import subprocess
|
197 |
import tempfile
|
198 |
-
|
199 |
-
#
|
200 |
converted_text = report_text.replace("\n", "<br>")
|
201 |
|
202 |
html_content = f"""
|
@@ -217,7 +223,7 @@ def generate_pdf_report(report_text: str) -> str:
|
|
217 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as html_file:
|
218 |
html_file.write(html_content.encode("utf-8"))
|
219 |
html_path = html_file.name
|
220 |
-
|
221 |
pdf_path = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
|
222 |
try:
|
223 |
subprocess.run(["wkhtmltopdf", html_path, pdf_path], check=True)
|
@@ -227,7 +233,7 @@ def generate_pdf_report(report_text: str) -> str:
|
|
227 |
pdf_path = ""
|
228 |
finally:
|
229 |
os.remove(html_path)
|
230 |
-
|
231 |
return pdf_path
|
232 |
|
233 |
def fetch_wikipedia_summary(topic: str) -> str:
|
|
|
16 |
import random
|
17 |
|
18 |
class DialogueItem(BaseModel):
|
19 |
+
speaker: Literal["Jane", "John"]
|
20 |
+
display_speaker: str = "Jane"
|
21 |
text: str
|
22 |
|
23 |
class Dialogue(BaseModel):
|
|
|
96 |
print(additional_info)
|
97 |
return additional_info
|
98 |
|
99 |
+
## Updated research_topic: Uses Tavily API for search/extraction and then GROQ API for generating the report.
|
100 |
def research_topic(topic: str) -> str:
|
101 |
+
print("[LOG] Researching topic using Tavily API and GROQ LLM:", topic)
|
102 |
tavily_api_key = os.environ.get("TAVILY_API_KEY")
|
103 |
if not tavily_api_key:
|
104 |
print("[ERROR] TAVILY_API_KEY not found in environment variables.")
|
|
|
164 |
"Make sure to include all relevant and updated information."
|
165 |
)
|
166 |
|
167 |
+
# Use GROQ API to generate the report
|
168 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
169 |
+
if not groq_api_key:
|
170 |
+
print("[ERROR] GROQ_API_KEY not found in environment variables.")
|
171 |
+
return "GROQ API key not configured."
|
172 |
+
|
173 |
+
groq_headers = {
|
174 |
+
"Authorization": f"Bearer {groq_api_key}",
|
175 |
"Content-Type": "application/json"
|
176 |
}
|
177 |
+
groq_data = {
|
178 |
+
"model": "DeepSeek-R1-Distill-Llama-70B",
|
179 |
"messages": [{"role": "user", "content": research_prompt}],
|
180 |
"max_tokens": 4096,
|
181 |
"temperature": 0.6
|
182 |
}
|
183 |
try:
|
184 |
+
groq_response = requests.post("https://api.groq.com/openai/v1/chat/completions",
|
185 |
+
headers=groq_headers, json=groq_data)
|
186 |
+
groq_response.raise_for_status()
|
187 |
+
response_json = groq_response.json()
|
188 |
report_content = response_json["choices"][0]["message"]["content"].strip()
|
189 |
+
print("[LOG] Research report generated successfully using GROQ API.")
|
190 |
except Exception as e:
|
191 |
+
print("[ERROR] GROQ API error during research report generation:", e)
|
192 |
report_content = f"Error generating research report: {str(e)}"
|
193 |
|
194 |
return report_content
|
195 |
|
196 |
+
## Function to generate a PDF report from text using wkhtmltopdf.
|
197 |
def generate_pdf_report(report_text: str) -> str:
|
198 |
"""
|
199 |
Generate a PDF file from the given report text using wkhtmltopdf.
|
|
|
201 |
"""
|
202 |
import subprocess
|
203 |
import tempfile
|
204 |
+
|
205 |
+
# Replace newlines with HTML line breaks.
|
206 |
converted_text = report_text.replace("\n", "<br>")
|
207 |
|
208 |
html_content = f"""
|
|
|
223 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as html_file:
|
224 |
html_file.write(html_content.encode("utf-8"))
|
225 |
html_path = html_file.name
|
226 |
+
|
227 |
pdf_path = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
|
228 |
try:
|
229 |
subprocess.run(["wkhtmltopdf", html_path, pdf_path], check=True)
|
|
|
233 |
pdf_path = ""
|
234 |
finally:
|
235 |
os.remove(html_path)
|
236 |
+
|
237 |
return pdf_path
|
238 |
|
239 |
def fetch_wikipedia_summary(topic: str) -> str:
|