Update app.py
Browse files
app.py
CHANGED
@@ -1,180 +1,184 @@
|
|
1 |
-
"""
|
2 |
-
CanRun Game Compatibility Checker - Simple MCP Server Implementation
|
3 |
-
"""
|
4 |
-
|
5 |
-
import gradio as gr
|
6 |
-
import logging
|
7 |
-
import os
|
8 |
-
import signal
|
9 |
-
import sys
|
10 |
-
import time
|
11 |
-
import asyncio
|
12 |
-
import base64
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
"
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
with
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
""
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
"
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
#
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
180 |
main()
|
|
|
1 |
+
"""
|
2 |
+
CanRun Game Compatibility Checker - Simple MCP Server Implementation
|
3 |
+
"""
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import logging
|
7 |
+
import os
|
8 |
+
import signal
|
9 |
+
import sys
|
10 |
+
import time
|
11 |
+
import asyncio
|
12 |
+
import base64
|
13 |
+
import platform
|
14 |
+
|
15 |
+
# Platform detection
|
16 |
+
IS_WINDOWS = platform.system() == "Windows"
|
17 |
+
from src.canrun_engine import CanRunEngine
|
18 |
+
from plugin import CanRunGAssistPlugin
|
19 |
+
|
20 |
+
# Configure logging
|
21 |
+
logging.basicConfig(
|
22 |
+
level=logging.INFO,
|
23 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
24 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
25 |
+
)
|
26 |
+
logger = logging.getLogger(__name__)
|
27 |
+
|
28 |
+
def signal_handler(signum, frame):
|
29 |
+
"""Handle shutdown signals gracefully."""
|
30 |
+
logger.info(f"Received signal {signum}, shutting down gracefully...")
|
31 |
+
sys.exit(0)
|
32 |
+
|
33 |
+
async def analyze_game(game_name):
|
34 |
+
"""Analyze game compatibility using the CanRun engine"""
|
35 |
+
if not game_name:
|
36 |
+
return "Please enter a game name to begin the analysis."
|
37 |
+
|
38 |
+
try:
|
39 |
+
plugin = CanRunGAssistPlugin()
|
40 |
+
params = {"game_name": game_name, "force_refresh": True}
|
41 |
+
# Properly await the async method
|
42 |
+
response = await plugin.check_game_compatibility(params)
|
43 |
+
|
44 |
+
if response.get("success", False):
|
45 |
+
return response.get("message", "Analysis completed successfully.")
|
46 |
+
else:
|
47 |
+
return response.get("message", "Could not analyze the game. Please check the game name and try again.")
|
48 |
+
except Exception as e:
|
49 |
+
logger.error(f"Error analyzing game: {e}")
|
50 |
+
return f"An error occurred during analysis: {e}"
|
51 |
+
|
52 |
+
def detect_hardware():
|
53 |
+
"""Detect hardware specifications"""
|
54 |
+
try:
|
55 |
+
plugin = CanRunGAssistPlugin()
|
56 |
+
response = plugin.detect_hardware({})
|
57 |
+
|
58 |
+
if response.get("success", False):
|
59 |
+
return response.get("message", "Hardware detection successful.")
|
60 |
+
else:
|
61 |
+
return response.get("message", "Could not detect hardware specifications.")
|
62 |
+
except Exception as e:
|
63 |
+
logger.error(f"Error detecting hardware: {e}")
|
64 |
+
return f"An error occurred during hardware detection: {e}"
|
65 |
+
|
66 |
+
def get_logo_html():
|
67 |
+
"""Get HTML that displays the logo"""
|
68 |
+
logo_path = os.path.join(os.path.dirname(__file__), "logo.png")
|
69 |
+
|
70 |
+
if os.path.exists(logo_path):
|
71 |
+
# Read the logo file and encode it as base64
|
72 |
+
with open(logo_path, "rb") as image_file:
|
73 |
+
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
|
74 |
+
|
75 |
+
# Return HTML that displays the logo
|
76 |
+
return f"""
|
77 |
+
<div style="display: flex; align-items: center; margin-bottom: 0.5em">
|
78 |
+
<img src="data:image/png;base64,{encoded_image}" alt="CanRun Logo" style="height: 4em; margin-right: 1em;">
|
79 |
+
<div>
|
80 |
+
<h1 style="margin: 0; padding: 0">CanRun Game Compatibility Checker</h1>
|
81 |
+
<p style="margin: 0; padding: 0">Check if your PC can run any game with an advanced tier system and Steam API integration</p>
|
82 |
+
</div>
|
83 |
+
</div>
|
84 |
+
"""
|
85 |
+
else:
|
86 |
+
logger.warning(f"Logo file not found at {logo_path}")
|
87 |
+
return """
|
88 |
+
<div>
|
89 |
+
<h1>CanRun Game Compatibility Checker</h1>
|
90 |
+
<p>Check if your PC can run any game with an advanced tier system and Steam API integration</p>
|
91 |
+
</div>
|
92 |
+
"""
|
93 |
+
|
94 |
+
def create_gradio_interface():
|
95 |
+
"""Create a simple Gradio interface with logo and favicon"""
|
96 |
+
# Set custom theme with brand color matching the logo
|
97 |
+
theme = gr.themes.Default(
|
98 |
+
primary_hue="green",
|
99 |
+
secondary_hue="gray",
|
100 |
+
)
|
101 |
+
|
102 |
+
# Define file paths
|
103 |
+
favicon_path = os.path.join(os.path.dirname(__file__), "logo.png")
|
104 |
+
|
105 |
+
with gr.Blocks(theme=theme, title="CanRun - Game Compatibility Checker", css="") as demo:
|
106 |
+
# Header with logo
|
107 |
+
gr.HTML(get_logo_html())
|
108 |
+
|
109 |
+
# Main content
|
110 |
+
with gr.Row():
|
111 |
+
with gr.Column():
|
112 |
+
game_input = gr.Textbox(label="Game Name", placeholder="Enter game name (e.g., Diablo 4)")
|
113 |
+
check_btn = gr.Button("Check Compatibility", variant="primary")
|
114 |
+
hw_btn = gr.Button("Detect Hardware", variant="secondary")
|
115 |
+
|
116 |
+
with gr.Column():
|
117 |
+
result_output = gr.Textbox(label="Results", lines=20)
|
118 |
+
|
119 |
+
# Footer
|
120 |
+
gr.HTML("""
|
121 |
+
<div style="margin-top: 20px; text-align: center; padding: 10px; border-top: 1px solid #ddd;">
|
122 |
+
<p>CanRun - Advanced Game Compatibility Checker with MCP Server Support</p>
|
123 |
+
<p>Powered by G-Assist Integration</p>
|
124 |
+
</div>
|
125 |
+
""")
|
126 |
+
|
127 |
+
# For async functions, we need to use .click(fn=..., inputs=..., outputs=...)
|
128 |
+
check_btn.click(fn=analyze_game, inputs=game_input, outputs=result_output)
|
129 |
+
hw_btn.click(fn=detect_hardware, inputs=None, outputs=result_output)
|
130 |
+
|
131 |
+
return demo
|
132 |
+
|
133 |
+
def is_mcp_available():
|
134 |
+
"""Check if the MCP package is available"""
|
135 |
+
try:
|
136 |
+
import mcp
|
137 |
+
return True
|
138 |
+
except ImportError:
|
139 |
+
return False
|
140 |
+
|
141 |
+
def main():
|
142 |
+
"""Main application entry point"""
|
143 |
+
# Set up signal handlers
|
144 |
+
signal.signal(signal.SIGINT, signal_handler)
|
145 |
+
signal.signal(signal.SIGTERM, signal_handler)
|
146 |
+
|
147 |
+
logger.info("Starting CanRun Game Compatibility Checker")
|
148 |
+
|
149 |
+
# Create Gradio interface
|
150 |
+
demo = create_gradio_interface()
|
151 |
+
|
152 |
+
# Check if MCP support is available
|
153 |
+
mcp_enabled = is_mcp_available()
|
154 |
+
if mcp_enabled:
|
155 |
+
logger.info("MCP server functionality is enabled")
|
156 |
+
else:
|
157 |
+
logger.info("MCP server functionality is disabled. Install with 'pip install \"gradio[mcp]\"' to enable")
|
158 |
+
|
159 |
+
# Launch with auto port discovery
|
160 |
+
launch_kwargs = {
|
161 |
+
"server_name": "0.0.0.0",
|
162 |
+
"share": False,
|
163 |
+
"favicon_path": os.path.join(os.path.dirname(__file__), "logo.png"),
|
164 |
+
}
|
165 |
+
|
166 |
+
# Only enable MCP server if the package is available
|
167 |
+
if mcp_enabled:
|
168 |
+
launch_kwargs["mcp_server"] = True
|
169 |
+
|
170 |
+
# Launch the server
|
171 |
+
demo.queue().launch(**launch_kwargs)
|
172 |
+
|
173 |
+
# Keep the main thread alive - Platform-independent approach
|
174 |
+
logger.info("Press Ctrl+C to stop the server")
|
175 |
+
try:
|
176 |
+
# This works on most systems and is more reliable than signal.pause()
|
177 |
+
while True:
|
178 |
+
time.sleep(1)
|
179 |
+
except KeyboardInterrupt:
|
180 |
+
logger.info("Keyboard interrupt received, shutting down...")
|
181 |
+
sys.exit(0)
|
182 |
+
|
183 |
+
if __name__ == "__main__":
|
184 |
main()
|