Commit
·
0910b82
1
Parent(s):
16cc21a
Revert "updated api respondes v2"
Browse filesThis reverts commit 4460d239a28e04c082a57a4fccb91453651a887d.
- mcp_server.py +10 -89
mcp_server.py
CHANGED
|
@@ -609,85 +609,6 @@ Sample analysis content..."""
|
|
| 609 |
|
| 610 |
return interface
|
| 611 |
|
| 612 |
-
# Create a FastAPI app for direct API access
|
| 613 |
-
app = gr.mount_gradio_app(
|
| 614 |
-
gr.routes.App(app=gr.Blocks()),
|
| 615 |
-
create_interface(),
|
| 616 |
-
path="/"
|
| 617 |
-
)
|
| 618 |
-
|
| 619 |
-
# Add a direct API endpoint for research
|
| 620 |
-
@app.post("/api/research")
|
| 621 |
-
async def api_research(request: dict):
|
| 622 |
-
try:
|
| 623 |
-
query = request.get('query', '')
|
| 624 |
-
if not query:
|
| 625 |
-
return {"error": "Query parameter is required"}, 400
|
| 626 |
-
|
| 627 |
-
# Initialize with default values or from environment variables
|
| 628 |
-
api_type = os.getenv("DEFAULT_API_TYPE", "Gemini")
|
| 629 |
-
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 630 |
-
gemini_model = os.getenv("GEMINI_MODEL", "gemini-2.0-flash")
|
| 631 |
-
tavily_key = os.getenv("TAVILY_API_KEY")
|
| 632 |
-
openrouter_key = os.getenv("OPENROUTER_API_KEY")
|
| 633 |
-
openrouter_model = os.getenv("OPENROUTER_MODEL", "anthropic/claude-3-opus:beta")
|
| 634 |
-
|
| 635 |
-
# Override with request values if provided
|
| 636 |
-
if 'api_type' in request:
|
| 637 |
-
api_type = request['api_type']
|
| 638 |
-
if 'gemini_key' in request:
|
| 639 |
-
gemini_key = request['gemini_key']
|
| 640 |
-
if 'gemini_model' in request:
|
| 641 |
-
gemini_model = request['gemini_model']
|
| 642 |
-
if 'tavily_key' in request:
|
| 643 |
-
tavily_key = request['tavily_key']
|
| 644 |
-
if 'openrouter_key' in request:
|
| 645 |
-
openrouter_key = request['openrouter_key']
|
| 646 |
-
if 'openrouter_model' in request:
|
| 647 |
-
openrouter_model = request['openrouter_model']
|
| 648 |
-
|
| 649 |
-
# Validate required API keys
|
| 650 |
-
if not tavily_key:
|
| 651 |
-
return {"error": "Tavily API key is required"}, 400
|
| 652 |
-
|
| 653 |
-
if api_type == "Gemini" and not gemini_key:
|
| 654 |
-
return {"error": "Gemini API key is required when using Gemini"}, 400
|
| 655 |
-
|
| 656 |
-
if api_type == "OpenRouter" and not openrouter_key:
|
| 657 |
-
return {"error": "OpenRouter API key is required when using OpenRouter"}, 400
|
| 658 |
-
|
| 659 |
-
# Initialize the system
|
| 660 |
-
system = MultiAgentSystem(
|
| 661 |
-
use_gemini=(api_type == "Gemini"),
|
| 662 |
-
gemini_api_key=gemini_key,
|
| 663 |
-
gemini_model=gemini_model,
|
| 664 |
-
tavily_api_key=tavily_key,
|
| 665 |
-
openrouter_api_key=openrouter_key if api_type == "OpenRouter" else None,
|
| 666 |
-
openrouter_model=openrouter_model if api_type == "OpenRouter" else None
|
| 667 |
-
)
|
| 668 |
-
|
| 669 |
-
# Process the query
|
| 670 |
-
markdown_text = system.process_query(query)
|
| 671 |
-
|
| 672 |
-
# Generate file paths
|
| 673 |
-
md_file_path = save_markdown_report(markdown_text)
|
| 674 |
-
html_file_path = convert_to_html(markdown_text)
|
| 675 |
-
|
| 676 |
-
# Return the results
|
| 677 |
-
return {
|
| 678 |
-
"query": query,
|
| 679 |
-
"result": markdown_text,
|
| 680 |
-
"downloads": {
|
| 681 |
-
"markdown": md_file_path,
|
| 682 |
-
"html": html_file_path
|
| 683 |
-
},
|
| 684 |
-
"status": "success"
|
| 685 |
-
}
|
| 686 |
-
|
| 687 |
-
except Exception as e:
|
| 688 |
-
server_logger.error(f"API research failed: {str(e)}", exc_info=True)
|
| 689 |
-
return {"error": f"Research failed: {str(e)}"}, 500
|
| 690 |
-
|
| 691 |
if __name__ == "__main__":
|
| 692 |
try:
|
| 693 |
# Configure event loop policy for Windows
|
|
@@ -697,15 +618,15 @@ if __name__ == "__main__":
|
|
| 697 |
if sys.version_info[0] == 3 and sys.version_info[1] >= 8:
|
| 698 |
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 699 |
|
| 700 |
-
server_logger.info("Starting server
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
except Exception as e:
|
| 710 |
-
server_logger.error(f"Failed to start server: {str(e)}", exc_info=True)
|
| 711 |
raise
|
|
|
|
| 609 |
|
| 610 |
return interface
|
| 611 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 612 |
if __name__ == "__main__":
|
| 613 |
try:
|
| 614 |
# Configure event loop policy for Windows
|
|
|
|
| 618 |
if sys.version_info[0] == 3 and sys.version_info[1] >= 8:
|
| 619 |
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 620 |
|
| 621 |
+
server_logger.info("Starting Gradio server")
|
| 622 |
+
interface = create_interface()
|
| 623 |
+
interface.launch(
|
| 624 |
+
server_name="0.0.0.0",
|
| 625 |
+
share=False,
|
| 626 |
+
debug=True,
|
| 627 |
+
prevent_thread_lock=True, # Allow for proper cleanup
|
| 628 |
+
mcp_server=True,
|
| 629 |
+
)
|
| 630 |
except Exception as e:
|
| 631 |
+
server_logger.error(f"Failed to start Gradio server: {str(e)}", exc_info=True)
|
| 632 |
raise
|