doshibhavya commited on
Commit
fbbd035
·
verified ·
1 Parent(s): 3f2a33b

Delete generate_server.py

Browse files
Files changed (1) hide show
  1. generate_server.py +0 -119
generate_server.py DELETED
@@ -1,119 +0,0 @@
1
- # generate_server.py
2
- from flask import Flask, request, jsonify
3
- import random, base64
4
- from datetime import datetime
5
- import io
6
-
7
- app = Flask(__name__)
8
-
9
- # Simple variation helpers
10
- DIRECT_TEMPLATES = [
11
- "{subject} stands in front of a body of water.",
12
- "{subject} is standing by the water, facing the camera.",
13
- "An {subject} stands near the water's edge, looking calm.",
14
- "{subject} stands at the bank of a water body under the sky.",
15
- "A lone {subject} stands before a reflective body of water."
16
- ]
17
-
18
- POETIC_TEMPLATES = [
19
- "Where light and shadow meet, {subject} stands before the mirrored water.",
20
- "Sunset breathes over the water while {subject} stands, a quiet poem in motion.",
21
- "By the water's hush, {subject} waits as nature hums its soft verses.",
22
- "Beneath the dwindling sun, {subject} becomes a silhouette of patient grace before the water."
23
- ]
24
-
25
- CREATIVE_TEMPLATES = [
26
- "In a quiet moment, {subject} pauses by the water — as if listening to old stories.",
27
- "{subject} and the water share a secret under the wide sky.",
28
- "An ordinary day turns gentle as {subject} meets the horizon of the water.",
29
- ]
30
-
31
- CAPTION_TEMPLATES = [
32
- "{subject} at the water — nature's calm moment.",
33
- "{subject} by the water. #serene #nature",
34
- "{subject} — a peaceful scene."
35
- ]
36
-
37
- # small function to pick a subject phrase by simple heuristics (could be replaced by image captioner)
38
- def quick_detect_subject_from_image(file_bytes):
39
- # QUICK fallback: don't try heavy vision here. For demo, return 'elephant' if filename hints or default 'subject'
40
- # If the request includes a filename you can inspect request.files['image'].filename
41
- return "elephant" # change if you have image caption code; keep simple for demo
42
-
43
- @app.route('/generate', methods=['POST'])
44
- def generate():
45
- # read fields
46
- style = request.form.get('style', 'direct').lower()
47
- variation = request.form.get('variation', 'false').lower() == 'true'
48
- request_id = request.form.get('request_id', '') # optional
49
- variation_seed = request.form.get('variation_seed')
50
- temperature = request.form.get('temperature') # optional hint
51
-
52
- # read uploaded image (optional: return it back as data-uri)
53
- image_data_uri = ""
54
- if 'image' in request.files:
55
- f = request.files['image']
56
- raw = f.read()
57
- # return the uploaded image back as base64 so client can display it (not required)
58
- try:
59
- image_data_uri = "data:image/jpeg;base64," + base64.b64encode(raw).decode('utf-8')
60
- except Exception:
61
- image_data_uri = ""
62
-
63
- # choose seed
64
- if variation_seed:
65
- try:
66
- seed = int(variation_seed) % (2**31 - 1)
67
- except:
68
- seed = int(datetime.utcnow().timestamp() * 1000) ^ (hash(request_id) & 0xffffffff)
69
- else:
70
- seed = int(datetime.utcnow().timestamp() * 1000) ^ (hash(request_id) & 0xffffffff)
71
-
72
- rnd = random.Random(seed)
73
-
74
- # Quick subject detection (replace with your model if available)
75
- subject = quick_detect_subject_from_image(raw if 'raw' in locals() else None)
76
-
77
- # Pick template set
78
- templates = DIRECT_TEMPLATES
79
- if style == 'poetic':
80
- templates = POETIC_TEMPLATES
81
- elif style == 'creative':
82
- templates = CREATIVE_TEMPLATES
83
- elif style == 'caption':
84
- templates = CAPTION_TEMPLATES
85
- else:
86
- templates = DIRECT_TEMPLATES
87
-
88
- # Variation: if variation requested, change selection logic to ensure different template often
89
- # Use rnd.choice to vary; because rnd seeded differently on each request, choice will differ
90
- chosen = rnd.choice(templates)
91
-
92
- # Small extra variation: maybe add an adjective randomly (for direct style keep minimal)
93
- adjectives = ["majestic", "gentle", "calm", "quiet", "stoic", "grand"]
94
- adj = ""
95
- if style != 'caption' and rnd.random() < 0.3:
96
- adj = rnd.choice(adjectives)
97
- # insert adjective if template supports
98
- if "{subject}" in chosen:
99
- chosen = chosen.replace("{subject}", f"{adj} {subject}")
100
- else:
101
- chosen = chosen.format(subject=subject)
102
-
103
- # ensure if any leftover placeholder exists
104
- try:
105
- story = chosen.format(subject=subject)
106
- except:
107
- story = chosen
108
-
109
- # Return JSON
110
- return jsonify({
111
- "success": True,
112
- "story": story,
113
- "image_data": image_data_uri,
114
- "request_id": request_id,
115
- "seed_used": seed
116
- })
117
-
118
- if __name__ == '__main__':
119
- app.run(debug=True, port=8000)