Trisha Tomy
undo
97d2a3d
raw
history blame
3.54 kB
import gevent.monkey
gevent.monkey.patch_all(asyncio=True)
# app.py
import asyncio
from flask import Flask, request, jsonify
from proxy_lite import Runner, RunnerConfig
import os
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
app = Flask(__name__)
_runner = None
async def initialize_runner():
global _runner
if _runner is None:
logger.info("Initializing Proxy-lite Runner...")
hf_api_token = os.environ.get("HF_API_TOKEN")
if not hf_api_token:
logger.error("HF_API_TOKEN environment variable not set. Cannot initialize Runner.")
raise ValueError("HF_API_TOKEN environment variable not set. Please set it as a Space secret.")
config = RunnerConfig.from_dict({
"environment": {
"name": "webbrowser",
"homepage": "https://www.google.com",
"headless": True,
},
"solver": {
"name": "simple",
"agent": {
"name": "proxy_lite",
"client": {
"name": "convergence",
"model_id": "convergence-ai/proxy-lite-3b",
"api_base": "https://api-inference.huggingface.co/models/convergence-ai/proxy-lite-3b",
"api_key": hf_api_token
}
}
}
})
_runner = Runner(config)
logger.info("Proxy-lite Runner initialized successfully.")
return _runner
# --- MODIFIED run_async_task FUNCTION ---
def run_async_task(coro):
"""
Helper to run async coroutines in a synchronous context (like Flask routes).
Ensures an event loop exists and runs the coroutine.
"""
try:
# Try to get the running loop (for current thread/greenlet)
loop = asyncio.get_running_loop()
except RuntimeError:
# If no loop is running, create a new one for this thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Run the coroutine until it completes
return loop.run_until_complete(coro)
# --- END MODIFIED run_async_task FUNCTION ---
@app.route('/run_proxy_task', methods=['POST'])
def run_proxy_task_endpoint():
data = request.json
task = data.get('task')
if not task:
logger.warning("Received request without 'task' field. Returning 400.")
return jsonify({"error": "No 'task' provided in request body"}), 400
logger.info(f"Received task for proxy-lite: '{task}'")
try:
runner = run_async_task(initialize_runner())
result = run_async_task(runner.run(task))
logger.info(f"Proxy-lite task completed. Output: {result[:200]}...")
return jsonify({"output": result})
except Exception as e:
logger.exception(f"Error processing task '{task}':")
return jsonify({"error": f"An error occurred: {str(e)}. Check logs for details."}), 500
@app.route('/')
def root():
logger.info("Root endpoint accessed.")
return "Proxy-lite API is running. Send POST requests to /run_proxy_task with a 'task' in JSON body."
if __name__ == '__main__':
if not os.environ.get("HF_API_TOKEN"):
logger.error("HF_API_TOKEN environment variable is not set. Please set it for local testing.")
exit(1)
logger.info("Starting Flask development server on 0.0.0.0:7860...")
app.run(host='0.0.0.0', port=7860, debug=True)