Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import subprocess | |
| import time | |
| from dotenv import load_dotenv | |
| import markdown | |
| import tempfile | |
| # Load environment variables | |
| load_dotenv() | |
| # Constants | |
| MAIN_PY_PATH = os.getenv('MAIN_PY_PATH', r"C:\Users\gusta\Desktop\latest_ai_development\src\latest_ai_development\main.py") | |
| REPORT_PATH = os.getenv('REPORT_PATH', r"C:\Users\gusta\Desktop\latest_ai_development\report.md") | |
| WORKING_DIR = os.getenv('WORKING_DIR', r"C:\Users\gusta\Desktop\latest_ai_development") | |
| # Custom CSS for better appearance | |
| custom_css = """ | |
| #component-0 { | |
| max-width: 1200px; | |
| margin: auto; | |
| } | |
| .gradio-container { | |
| font-family: 'Arial', sans-serif; | |
| } | |
| .logo { | |
| text-align: center; | |
| font-size: 2.5em; | |
| font-weight: bold; | |
| background: linear-gradient(45deg, #00bfa5, #2979ff, #ec407a); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| margin-bottom: 1em; | |
| } | |
| .container { | |
| background: white; | |
| padding: 2em; | |
| border-radius: 10px; | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| } | |
| """ | |
| def update_topic_in_main(topic): | |
| """Update the topic in main.py file""" | |
| try: | |
| with open(MAIN_PY_PATH, 'r', encoding='utf-8') as file: | |
| lines = file.readlines() | |
| for i, line in enumerate(lines): | |
| if "'topic':" in line: | |
| indent = len(line) - len(line.lstrip()) | |
| lines[i] = ' ' * indent + f"'topic': '{topic}'\n" | |
| break | |
| with open(MAIN_PY_PATH, 'w', encoding='utf-8') as file: | |
| file.writelines(lines) | |
| return True | |
| except Exception as e: | |
| print(f"Error updating topic: {str(e)}") | |
| return False | |
| def delete_old_report(): | |
| """Delete the old report if it exists""" | |
| try: | |
| if os.path.exists(REPORT_PATH): | |
| os.remove(REPORT_PATH) | |
| return True | |
| except Exception as e: | |
| print(f"Error deleting old report: {str(e)}") | |
| return False | |
| def run_research(topic, progress=gr.Progress()): | |
| """Main research function""" | |
| try: | |
| # Input validation | |
| if not topic or len(topic.strip()) < 10: | |
| return "Please enter a more detailed research topic (at least 10 characters)." | |
| # Update topic in main.py | |
| if not update_topic_in_main(topic): | |
| return "Error: Could not update research topic." | |
| # Delete old report | |
| if not delete_old_report(): | |
| return "Error: Could not prepare for new research." | |
| # Change directory and run crewai | |
| os.chdir(WORKING_DIR) | |
| # Show progress updates | |
| progress(0, desc="Starting research...") | |
| process = subprocess.Popen( | |
| 'crewai run', | |
| shell=True, | |
| cwd=WORKING_DIR, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| text=True | |
| ) | |
| # Wait for process to complete with timeout | |
| try: | |
| process.wait(timeout=300) # 5 minutes timeout | |
| except subprocess.TimeoutExpired: | |
| process.kill() | |
| return "Error: Research process timed out. Please try again." | |
| # Check for report | |
| for _ in range(30): # Check for 30 seconds | |
| progress(0.5, desc="Generating report...") | |
| if os.path.exists(REPORT_PATH): | |
| with open(REPORT_PATH, 'r', encoding='utf-8') as file: | |
| content = file.read().strip() | |
| if content: | |
| # Convert markdown to HTML for better display | |
| html_content = markdown.markdown(content) | |
| return html_content | |
| time.sleep(1) | |
| return "Error: Could not generate report. Please try again." | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Create Gradio interface | |
| def create_interface(): | |
| with gr.Blocks(css=custom_css) as demo: | |
| gr.HTML("<div class='logo'>OneAI</div>") | |
| gr.HTML("<div style='text-align: center; margin-bottom: 1em;'><h3>Your OnePage AI</h3><p>Everything in The World in One Page</p></div>") | |
| with gr.Column(elem_classes="container"): | |
| topic_input = gr.Textbox( | |
| label="Research Topic", | |
| placeholder="Enter Your Research Topic e.g. Detail Analysis of US Inflation Market in 2025 or Detail Analysis of Worldwide Earthquakes", | |
| lines=2 | |
| ) | |
| submit_btn = gr.Button("Start Research", variant="primary") | |
| output = gr.HTML(label="Results") | |
| submit_btn.click( | |
| fn=run_research, | |
| inputs=topic_input, | |
| outputs=output | |
| ) | |
| return demo | |
| # Launch the interface | |
| if __name__ == "__main__": | |
| demo = create_interface() | |
| demo.queue() # Enable queuing | |
| demo.launch( | |
| share=True, | |
| show_error=True, | |
| server_name="0.0.0.0", | |
| server_port=7860 # Default Gradio port | |
| ) | |