legends810 commited on
Commit
f4f1ef0
·
verified ·
1 Parent(s): afb8a07

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request
2
+ import subprocess
3
+ import json
4
+ import os
5
+
6
+ app = Flask(__name__)
7
+
8
+ @app.route('/')
9
+ def home():
10
+ return """
11
+ <h1>MCPollinations MCP Server</h1>
12
+ <p>Server is running on Hugging Face Spaces!</p>
13
+ <p>This is a Model Context Protocol server for AI image and text generation.</p>
14
+ <h2>Available endpoints:</h2>
15
+ <ul>
16
+ <li><a href="/status">/status</a> - Check server status</li>
17
+ <li>/generate_image - Generate images via POST</li>
18
+ <li>/generate_text - Generate text via POST</li>
19
+ </ul>
20
+ """
21
+
22
+ @app.route('/status')
23
+ def status():
24
+ return jsonify({
25
+ "status": "running",
26
+ "server": "MCPollinations MCP Server",
27
+ "mode": "HTTP wrapper for HF Spaces"
28
+ })
29
+
30
+ @app.route('/generate_image', methods=['POST'])
31
+ def generate_image():
32
+ try:
33
+ data = request.get_json()
34
+ prompt = data.get('prompt', 'sunset over ocean')
35
+
36
+ # Run MCP command (simplified example)
37
+ result = {
38
+ "status": "success",
39
+ "prompt": prompt,
40
+ "image_url": f"https://image.pollinations.ai/{prompt}",
41
+ "message": "Image generation request processed"
42
+ }
43
+ return jsonify(result)
44
+ except Exception as e:
45
+ return jsonify({"error": str(e)}), 500
46
+
47
+ @app.route('/generate_text', methods=['POST'])
48
+ def generate_text():
49
+ try:
50
+ data = request.get_json()
51
+ prompt = data.get('prompt', 'Hello world')
52
+
53
+ result = {
54
+ "status": "success",
55
+ "prompt": prompt,
56
+ "message": "Text generation endpoint (implement as needed)"
57
+ }
58
+ return jsonify(result)
59
+ except Exception as e:
60
+ return jsonify({"error": str(e)}), 500
61
+
62
+ if __name__ == '__main__':
63
+ port = int(os.environ.get('PORT', 7860))
64
+ app.run(host='0.0.0.0', port=port, debug=False)