Spaces:
Sleeping
Sleeping
Commit ·
bbe93df
1
Parent(s): eced25c
fix: change json loarder
Browse files
server.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import logging
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
import
|
| 6 |
|
| 7 |
from app.util.gen_ai_base import GenAIBaseClient
|
| 8 |
from app.util.browser_agent import BrowserAgent
|
|
@@ -17,32 +17,42 @@ def create_app() -> Flask:
|
|
| 17 |
|
| 18 |
@app.route('/scrape', methods=['POST'])
|
| 19 |
async def scrape():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
api_key = os.getenv("GOOGLE_AI_STUDIO_API_KEY")
|
| 30 |
-
explorer = GenAIBaseClient(api_key=api_key)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
-
|
|
|
|
| 39 |
return jsonify({"error": str(e)}), 500
|
| 40 |
-
response_data = {
|
| 41 |
-
"link_map": {href: node.model_dump() for href, node in agent.link_map.items()},
|
| 42 |
-
"token_usage": explorer.token_usage
|
| 43 |
-
}
|
| 44 |
-
return jsonify(response_data), 200
|
| 45 |
-
|
| 46 |
@app.route('/', methods=['GET'])
|
| 47 |
def hello_world():
|
| 48 |
return "Flask server is running.", 200
|
|
|
|
| 2 |
import logging
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
from app.util.gen_ai_base import GenAIBaseClient
|
| 8 |
from app.util.browser_agent import BrowserAgent
|
|
|
|
| 17 |
|
| 18 |
@app.route('/scrape', methods=['POST'])
|
| 19 |
async def scrape():
|
| 20 |
+
try:
|
| 21 |
+
raw = request.get_data(as_text=True)
|
| 22 |
+
print("Raw body:", raw)
|
| 23 |
|
| 24 |
+
# Manual parse so Content-Type doesn't matter
|
| 25 |
+
body = json.loads(raw)
|
| 26 |
+
logging.info(f"Headers: {dict(request.headers)}")
|
| 27 |
+
logging.info(f"Raw data: {request.data}")
|
| 28 |
|
| 29 |
+
url = body.get('url')
|
| 30 |
+
max_depth = body.get('max_depth', 2)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
if not url:
|
| 33 |
+
return jsonify({"error": "URL is required"}), 400
|
| 34 |
+
api_key = os.getenv("GOOGLE_AI_STUDIO_API_KEY")
|
| 35 |
+
explorer = GenAIBaseClient(api_key=api_key)
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
async with BrowserAgent(model=explorer, max_depth=max_depth) as agent:
|
| 39 |
+
root_node = await agent.run(start_url=url)
|
| 40 |
+
if not root_node:
|
| 41 |
+
return jsonify({"error": "Exploration failed or returned no data"}), 500
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logging.error(f"Error during scraping: {e}")
|
| 44 |
+
return jsonify({"error": str(e)}), 500
|
| 45 |
+
response_data = {
|
| 46 |
+
"link_map": {href: node.model_dump() for href, node in agent.link_map.items()},
|
| 47 |
+
"token_usage": explorer.token_usage
|
| 48 |
+
}
|
| 49 |
+
return jsonify(response_data), 200
|
| 50 |
+
except json.JSONDecodeError as e:
|
| 51 |
+
return jsonify({"error": f"Invalid JSON: {e}"}), 400
|
| 52 |
except Exception as e:
|
| 53 |
+
import traceback
|
| 54 |
+
traceback.print_exc()
|
| 55 |
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
@app.route('/', methods=['GET'])
|
| 57 |
def hello_world():
|
| 58 |
return "Flask server is running.", 200
|