33try :
44 from flask import Flask , request , jsonify , render_template_string
55 from flask_cors import CORS
6+
67 FLASK_AVAILABLE = True
78except ImportError :
89 FLASK_AVAILABLE = False
254255"""
255256
256257
257- def run_api_server (host = ' 0.0.0.0' , port = 5000 , debug = False ):
258+ def run_api_server (host = " 0.0.0.0" , port = 5000 , debug = False ):
258259 """Run the API server"""
259260 if not FLASK_AVAILABLE :
260- print ("Error: Flask is not installed. Install it with: pip install flask flask-cors" )
261+ print (
262+ "Error: Flask is not installed. Install it with: pip install flask flask-cors"
263+ )
261264 return
262265
263266 app = Flask (__name__ )
264267 CORS (app ) # Enable CORS for all routes
265268
266- @app .route ('/' )
269+ @app .route ("/" )
267270 def index ():
268271 """Serve the web interface"""
269272 return render_template_string (HTML_TEMPLATE )
270273
271- @app .route (' /api/translate' , methods = [' POST' ])
274+ @app .route (" /api/translate" , methods = [" POST" ])
272275 def translate_command ():
273276 """Translate a command via API"""
274277 try :
275278 data = request .get_json ()
276- command = data .get (' command' , '' ).strip ()
277- direction = data .get (' direction' , ' lnx2ps' )
279+ command = data .get (" command" , "" ).strip ()
280+ direction = data .get (" direction" , " lnx2ps" )
278281
279282 if not command :
280- return jsonify ({' error' : ' No command provided' }), 400
283+ return jsonify ({" error" : " No command provided" }), 400
281284
282285 # Try plugin translation first
283- plugin_translation = plugin_manager .translate_with_plugins (command , direction )
286+ plugin_translation = plugin_manager .translate_with_plugins (
287+ command , direction
288+ )
284289
285290 # Try ML translation
286291 ml_translation = ml_engine .get_best_translation (command , direction )
287292
288293 # Use core translation
289- if direction == ' lnx2ps' :
294+ if direction == " lnx2ps" :
290295 translation = lnx2ps (command )
291296 else :
292297 translation = ps2lnx (command )
293298
294299 # Prefer plugin translation, then ML, then core
295300 if plugin_translation :
296301 final_translation = plugin_translation
297- source = ' plugin'
302+ source = " plugin"
298303 elif ml_translation :
299304 final_translation = ml_translation
300- source = 'ml'
305+ source = "ml"
301306 else :
302307 final_translation = translation
303- source = ' core'
308+ source = " core"
304309
305310 # Learn the pattern
306311 ml_engine .learn_pattern (command , final_translation , direction , success = True )
@@ -309,93 +314,98 @@ def translate_command():
309314 suggestions = ml_engine .get_suggestions (command , direction , limit = 3 )
310315 suggestion_texts = [s [0 ] for s in suggestions ]
311316
312- return jsonify ({
313- 'translation' : final_translation ,
314- 'source' : source ,
315- 'suggestions' : suggestion_texts ,
316- 'command' : command ,
317- 'direction' : direction
318- })
317+ return jsonify (
318+ {
319+ "translation" : final_translation ,
320+ "source" : source ,
321+ "suggestions" : suggestion_texts ,
322+ "command" : command ,
323+ "direction" : direction ,
324+ }
325+ )
319326
320327 except Exception as e :
321- return jsonify ({' error' : str (e )}), 500
328+ return jsonify ({" error" : str (e )}), 500
322329
323- @app .route (' /api/stats' )
330+ @app .route (" /api/stats" )
324331 def get_stats ():
325332 """Get translation statistics"""
326333 try :
327334 analysis = ml_engine .analyze_patterns ()
328335
329- return jsonify ({
330- 'total_translations' : analysis .get ('total_patterns' , 0 ),
331- 'success_rate' : analysis .get ('success_rate' , 0 ),
332- 'learned_patterns' : analysis .get ('total_patterns' , 0 ),
333- 'command_types' : analysis .get ('command_types' , {}),
334- 'top_patterns' : analysis .get ('top_successful_patterns' , [])
335- })
336+ return jsonify (
337+ {
338+ "total_translations" : analysis .get ("total_patterns" , 0 ),
339+ "success_rate" : analysis .get ("success_rate" , 0 ),
340+ "learned_patterns" : analysis .get ("total_patterns" , 0 ),
341+ "command_types" : analysis .get ("command_types" , {}),
342+ "top_patterns" : analysis .get ("top_successful_patterns" , []),
343+ }
344+ )
336345
337346 except Exception as e :
338- return jsonify ({' error' : str (e )}), 500
347+ return jsonify ({" error" : str (e )}), 500
339348
340- @app .route (' /api/plugins' )
349+ @app .route (" /api/plugins" )
341350 def list_plugins ():
342351 """List available plugins"""
343352 try :
344353 plugins = plugin_manager .list_plugins ()
345- return jsonify ({' plugins' : plugins })
354+ return jsonify ({" plugins" : plugins })
346355
347356 except Exception as e :
348- return jsonify ({' error' : str (e )}), 500
357+ return jsonify ({" error" : str (e )}), 500
349358
350- @app .route (' /api/plugins/<plugin_name>' )
359+ @app .route (" /api/plugins/<plugin_name>" )
351360 def get_plugin_info (plugin_name ):
352361 """Get information about a specific plugin"""
353362 try :
354363 if plugin_name in plugin_manager .plugins :
355364 plugin = plugin_manager .plugins [plugin_name ]
356365 return jsonify (plugin .get_metadata ())
357366 else :
358- return jsonify ({' error' : ' Plugin not found' }), 404
367+ return jsonify ({" error" : " Plugin not found" }), 404
359368
360369 except Exception as e :
361- return jsonify ({' error' : str (e )}), 500
370+ return jsonify ({" error" : str (e )}), 500
362371
363- @app .route (' /api/learn' , methods = [' POST' ])
372+ @app .route (" /api/learn" , methods = [" POST" ])
364373 def learn_pattern ():
365374 """Manually learn a pattern"""
366375 try :
367376 data = request .get_json ()
368- command = data .get (' command' )
369- translation = data .get (' translation' )
370- direction = data .get (' direction' )
371- success = data .get (' success' , True )
377+ command = data .get (" command" )
378+ translation = data .get (" translation" )
379+ direction = data .get (" direction" )
380+ success = data .get (" success" , True )
372381
373382 if not all ([command , translation , direction ]):
374- return jsonify ({' error' : ' Missing required fields' }), 400
383+ return jsonify ({" error" : " Missing required fields" }), 400
375384
376385 ml_engine .learn_pattern (command , translation , direction , success )
377- return jsonify ({' message' : ' Pattern learned successfully' })
386+ return jsonify ({" message" : " Pattern learned successfully" })
378387
379388 except Exception as e :
380- return jsonify ({' error' : str (e )}), 500
389+ return jsonify ({" error" : str (e )}), 500
381390
382- @app .route (' /api/cleanup' , methods = [' POST' ])
391+ @app .route (" /api/cleanup" , methods = [" POST" ])
383392 def cleanup_patterns ():
384393 """Clean up old patterns"""
385394 try :
386395 data = request .get_json ()
387- days = data .get (' days' , 30 )
396+ days = data .get (" days" , 30 )
388397
389398 ml_engine .cleanup_old_patterns (days )
390- return jsonify ({' message' : f' Cleaned up patterns older than { days } days' })
399+ return jsonify ({" message" : f" Cleaned up patterns older than { days } days" })
391400
392401 except Exception as e :
393- return jsonify ({' error' : str (e )}), 500
402+ return jsonify ({" error" : str (e )}), 500
394403
395404 print (f"Starting ShellRosetta API server on http://{ host } :{ port } " )
396405 print ("Press Ctrl+C to stop the server" )
397406
398407 app .run (host = host , port = port , debug = debug )
399408
400- if __name__ == '__main__' :
409+
410+ if __name__ == "__main__" :
401411 run_api_server ()
0 commit comments