AdarshJi commited on
Commit
4e87f73
·
verified ·
1 Parent(s): 2fe096a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py CHANGED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from g4f.Provider.hf_space import BlackForestLabsFlux1Dev, G4F, BlackForestLabsFlux1Schnell
2
+ import asyncio
3
+ from flask import Flask, request, jsonify, Response, render_template
4
+
5
+ app = Flask(__name__)
6
+
7
+
8
+ class IMG:
9
+ def __init__(self, prompt: str, width: int = 1024, height: int = 1024, guidance_scale: float = 3.5, seed: int = 0):
10
+ self.prompt = prompt
11
+ self.width = width
12
+ self.height = height
13
+ self.guidance_scale = guidance_scale
14
+ self.seed = seed
15
+ self.messages = [{"role": "user", "content": self.prompt}]
16
+
17
+ async def _run_async_generator(self, generator):
18
+ """Runs the async generator and extracts image URLs safely."""
19
+ results = []
20
+ try:
21
+ async for result in generator:
22
+ if hasattr(result, "images") and isinstance(result.images, list):
23
+ results.extend(result.images)
24
+ else:
25
+ results.append(str(result)) # Convert non-image responses to string
26
+ except Exception as e:
27
+ print("Error processing response:", e)
28
+ return results
29
+
30
+ def _generate_images(self, provider_class, model):
31
+ """Generic method to fetch images from any provider."""
32
+ async def main():
33
+ try:
34
+ async for result in provider_class.create_async_generator(
35
+ model=model, messages=self.messages,
36
+ width=self.width, height=self.height,
37
+ guidance_scale=self.guidance_scale, seed=self.seed
38
+ ):
39
+ yield result
40
+ except Exception as e:
41
+ print(f"Error generating images from {model}:", e)
42
+ yield f"Error: {e}"
43
+
44
+ return asyncio.run(self._run_async_generator(main()))
45
+
46
+ def BlackForest(self,model="black-forest-labs-flux-1-dev"):
47
+ if model in BlackForestLabsFlux1Dev.get_models():
48
+ pass
49
+ else:
50
+ model = "black-forest-labs-flux-1-dev"
51
+ return self._generate_images(BlackForestLabsFlux1Dev, model)
52
+
53
+ def FluxMidJourny(self,model="flux"):
54
+ if model in G4F.get_models():
55
+ pass
56
+ else:
57
+ model = "flux"
58
+ return self._generate_images(G4F, model)
59
+
60
+ def BlackForestSchnell(self,model="black-forest-labs-flux-1-schnell"):
61
+ if model in BlackForestLabsFlux1Schnell.get_models():
62
+ pass
63
+ else:
64
+ model = "black-forest-labs-flux-1-schnell"
65
+ return self._generate_images(BlackForestLabsFlux1Schnell, model)
66
+
67
+
68
+ @app.route("/generate/image", methods=["POST"])
69
+ def generate_image():
70
+ data = request.json
71
+ prompt = data.get("prompt")
72
+ model = data.get("model", "black-forest-labs-flux-1-dev")
73
+ width = data.get("width", 1024)
74
+ height = data.get("height", 1024)
75
+ guidance_scale = data.get("guidance_scale", 3.5)
76
+ seed = data.get("seed", 0)
77
+ provider = data.get("provider", "flux")
78
+
79
+ if not prompt:
80
+ return jsonify({"error": "prompt is required"}), 400
81
+
82
+ def GenerateImage():
83
+ img = IMG(prompt, width, height, guidance_scale, seed)
84
+ if provider == "blackforestlabs":
85
+ return img.BlackForest(model)
86
+ elif provider == "flux":
87
+ return img.FluxMidJourny(model)
88
+ elif provider == "blackforestlabs-schnell":
89
+ return img.BlackForestSchnell(model)
90
+
91
+ result = GenerateImage()
92
+ print(result)
93
+ return jsonify({"Result" : result}), 200
94
+
95
+ @app.route("/providers", methods=["GET"])
96
+ def get_providers():
97
+ return jsonify({"providers": ["blackforestlabs", "flux", "blackforestlabs-schnell"]}), 200
98
+
99
+ @app.route("/generate/image/model", methods=["POST"])
100
+ def get_models():
101
+ data = request.json
102
+ provider = data.get("provider", "blackforestlabs")
103
+
104
+ if provider == "blackforestlabs":
105
+ return jsonify({"models": BlackForestLabsFlux1Dev.get_models()}), 200
106
+ elif provider == "flux":
107
+ return jsonify({"models": G4F.get_models()}), 200
108
+ elif provider == "blackforestlabs-schnell":
109
+ return jsonify({"models": BlackForestLabsFlux1Schnell.get_models()}), 200
110
+ return jsonify({"error": "provider not found"}), 404
111
+
112
+ @app.route("/", methods=["GET"])
113
+ def index():
114
+ return render_template("index.html")
115
+
116
+ if __name__ == "__main__":
117
+ app.run(host="0.0.0.0", port=7860)