| import os |
| import re |
| import argparse |
| import datetime |
| from fpdf import FPDF |
| from google import genai |
|
|
| |
|
|
| client = genai.Client(api_key="AIzaSyBk2-1rHDmRgKKW5WfLQ5T1tcL38jPmJAc") |
|
|
| |
| DEFAULT_PROMPTS = { |
| |
| |
| |
| |
| |
| "2. B2B Textile & Material Innovation": """ |
| Investigate the B2B textile market for current material innovations and commercial fiber developments. |
| Focus heavily on the adoption of sustainable fabrics, recycled blends, and technical/performance textiles (e.g., moisture-wicking, temperature-regulating). |
| Cite specific yarn and fabric types that are currently experiencing the highest sourcing demand from global brands. |
| """ |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
|
|
| def clean_markdown_for_pdf(text): |
| """Removes markdown formatting and strips all source/citation references.""" |
| text = re.sub(r'\*\*(.*?)\*\*', r'\1', text) |
| text = re.sub(r'\*(.*?)\*', r'\1', text) |
| text = re.sub(r'#+(.*?)\n', r'\1\n', text) |
|
|
| |
| text = re.sub(r'\[\d+(?:,\s*\d+)*\]', '', text) |
|
|
| |
| |
| text = re.sub( |
| r'\n\s*(?:#{1,3}\s*)?(?:Sources|References|Bibliography|Works Cited|Citations|Further Reading)' |
| r'.*', |
| '', |
| text, |
| flags=re.IGNORECASE | re.DOTALL |
| ) |
|
|
| return text.strip() |
|
|
|
|
| def generate_deep_research_report(custom_query: str = None, topic: str = "Textile_Report"): |
| |
| base_dir = os.path.dirname(os.path.abspath(__file__)) |
| output_dir = os.path.join("/Volumes/ssd2/TEXBASE/src/ResearchAgent/", "research_pdf") |
| os.makedirs(output_dir, exist_ok=True) |
|
|
| pdf = FPDF() |
| pdf.set_auto_page_break(auto=True, margin=15) |
|
|
| print("Starting Deep Research Agent. This will take several minutes per section...\n") |
|
|
| if custom_query: |
| prompts = {topic: custom_query} |
| else: |
| prompts = DEFAULT_PROMPTS |
|
|
| for section_title, prompt in prompts.items(): |
| print(f"\n{'='*50}") |
| print(f"-> Initiating Research for: {section_title}") |
| print(f"{'='*50}\n") |
|
|
| |
| stream = client.interactions.create( |
| input=prompt, |
| agent="deep-research-pro-preview-12-2025", |
| background=True, |
| stream=True, |
| agent_config={ |
| "type": "deep-research", |
| "thinking_summaries": "auto" |
| } |
| ) |
|
|
| interaction_id = None |
| section_text = "" |
|
|
| |
| for chunk in stream: |
| if chunk.event_type == "interaction.start": |
| interaction_id = chunk.interaction.id |
| print(f"[System] Interaction started: {interaction_id}") |
|
|
| elif chunk.event_type == "content.delta": |
| |
| if chunk.delta.type == "text": |
| section_text += chunk.delta.text |
|
|
| |
| elif chunk.delta.type == "thought_summary": |
| print(f"Thought: {chunk.delta.content.text}", flush=True) |
|
|
| elif chunk.event_type == "interaction.complete": |
| print(f"\n[System] Research Complete for {section_title}") |
|
|
| |
| pdf.add_page() |
|
|
| |
| pdf.set_font("helvetica", style="B", size=16) |
| pdf.cell(0, 10, txt=section_title, ln=True, align='L') |
| pdf.ln(5) |
|
|
| |
| pdf.set_font("helvetica", size=11) |
| clean_text = clean_markdown_for_pdf(section_text) |
|
|
| |
| safe_text = clean_text.encode('latin-1', 'replace').decode('latin-1') |
| pdf.multi_cell(0, 6, txt=safe_text) |
|
|
| |
| safe_topic = re.sub(r'[^A-Za-z0-9_\-]', '_', topic) |
| file_path = os.path.join(output_dir, f"{safe_topic}.pdf") |
| pdf.output(file_path) |
|
|
| print(f"\n{'='*50}") |
| print(f"Success! Full deep research report saved to: {file_path}") |
| print(f"{'='*50}") |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Run Deep Research Agent") |
| parser.add_argument("--query", type=str, help="Custom research query to extract") |
| parser.add_argument("--topic", type=str, default="Deep_Research_Textile_Report", help="Short specific topic name used for the output filename") |
| args = parser.parse_args() |
|
|
| generate_deep_research_report(custom_query=args.query, topic=args.topic) |
| |
| |
| os._exit(0) |
|
|