-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
107 lines (85 loc) · 3.68 KB
/
app.py
File metadata and controls
107 lines (85 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import requests
import os
app = Flask(__name__, static_folder='static')
CORS(app)
print("\n" + "="*80)
print("🚀 MISTRAL MODEL - Frontend Server")
print("="*80)
print("API URL will be provided from frontend settings")
print("="*80 + "\n")
# Serve static files
@app.route('/')
def index():
return send_from_directory('static', 'index.html')
@app.route('/static/<path:path>')
def serve_static(path):
return send_from_directory('static', path)
# Health check endpoint
@app.route('/api/health', methods=['GET'])
def health():
return jsonify({
'status': 'ok',
'message': 'Frontend server is running'
})
# Generate endpoint - proxies to Colab API
@app.route('/api/generate', methods=['POST'])
def generate():
import time
try:
data = request.json
prompt = data.get('prompt', '')
max_new_tokens = data.get('max_new_tokens', 2048)
temperature = data.get('temperature', 0.5)
top_p = data.get('top_p', 0.9)
colab_api_url = data.get('colab_api_url', '')
if not prompt:
return jsonify({'error': 'No prompt provided'}), 400
if not colab_api_url:
return jsonify({'error': 'API URL not configured. Please set it in the frontend settings.'}), 400
print(f"\n{'='*80}", flush=True)
print(f"🚀 NEW REQUEST - LEGAL DOCUMENT GENERATION", flush=True)
print(f"Prompt length: {len(prompt)} characters", flush=True)
print(f"Max tokens requested: {max_new_tokens}", flush=True)
print(f"Forwarding to Colab API: {colab_api_url}", flush=True)
start_time = time.time()
# Forward request to Colab API (using /api/generate endpoint)
response = requests.post(
f"{colab_api_url}/api/generate",
json={
'prompt': prompt,
'max_new_tokens': max_new_tokens,
'temperature': temperature,
'top_p': top_p
},
timeout=600 # 10 minutes timeout
)
if response.status_code != 200:
error_msg = f"Colab API error: {response.status_code}"
print(f"❌ {error_msg}", flush=True)
return jsonify({'error': error_msg}), 500
result = response.json()
generated_text = result.get('generated_text', '')
elapsed = time.time() - start_time
print(f"✅ Generated in {elapsed:.2f}s", flush=True)
print(f"📊 Response length: {len(generated_text)} characters", flush=True)
print(f"\n📄 OUTPUT:", flush=True)
print(f"{'-'*80}", flush=True)
print(generated_text[:500] + "..." if len(generated_text) > 500 else generated_text, flush=True)
print(f"{'-'*80}\n", flush=True)
return jsonify({'generated_text': generated_text})
except requests.exceptions.Timeout:
print(f"❌ ERROR: Request timeout after 10 minutes", flush=True)
return jsonify({'error': 'Request took longer than 10 minutes'}), 504
except requests.exceptions.ConnectionError:
error_msg = 'Cannot connect to Colab API - Check if notebook is running and URL is correct'
print(f"❌ ERROR: {error_msg}", flush=True)
return jsonify({'error': error_msg}), 503
except Exception as e:
print(f"❌ ERROR: {str(e)}", flush=True)
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
port = int(os.environ.get('PORT', 7860))
print(f"🌐 Starting server on http://localhost:{port}...\n")
app.run(host='0.0.0.0', port=port, debug=False)