Update myapp.py
Browse files
myapp.py
CHANGED
@@ -1,71 +1,42 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
from diffusers import DiffusionPipeline
|
3 |
import torch
|
4 |
-
from
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
|
9 |
# Initialize the Flask app
|
10 |
-
|
11 |
|
12 |
-
# Load the
|
13 |
-
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
return '''
|
18 |
-
<html>
|
19 |
-
<body>
|
20 |
-
<h1>Welcome to the Image Generation API!</h1>
|
21 |
-
<form id="input-form">
|
22 |
-
<label for="prompt">Enter your prompt:</label><br>
|
23 |
-
<input type="text" id="prompt" name="prompt"><br><br>
|
24 |
-
<button type="submit">Generate Image</button>
|
25 |
-
</form>
|
26 |
-
<div id="spinner" style="display:none;">Generating image, please wait...</div>
|
27 |
-
<div id="result"></div>
|
28 |
-
<script>
|
29 |
-
document.getElementById('input-form').onsubmit = async (e) => {
|
30 |
-
e.preventDefault();
|
31 |
-
document.getElementById('spinner').style.display = 'block';
|
32 |
-
const prompt = document.getElementById('prompt').value;
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
headers: { 'Content-Type': 'application/json' },
|
37 |
-
body: JSON.stringify({ prompt })
|
38 |
-
});
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
}
|
47 |
-
};
|
48 |
-
</script>
|
49 |
-
</body>
|
50 |
-
</html>
|
51 |
-
'''
|
52 |
|
53 |
-
@
|
54 |
-
def
|
55 |
data = request.json
|
56 |
-
prompt = data.get('prompt', '
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
|
64 |
-
pil_image.save(output_path)
|
65 |
|
66 |
-
#
|
67 |
-
return jsonify(
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
-
|
71 |
-
myapp.run(host='0.0.0.0', port=7860)
|
|
|
1 |
from flask import Flask, request, jsonify
|
|
|
2 |
import torch
|
3 |
+
from transformers import AutoModel, AutoTokenizer
|
4 |
+
from fastsafetensors import safe_load
|
|
|
|
|
5 |
|
6 |
# Initialize the Flask app
|
7 |
+
app = Flask(__name__)
|
8 |
|
9 |
+
# Load the model and tokenizer using safe_load
|
10 |
+
model_path = "https://huggingface.co/prompthero/openjourney-v4/blob/main/safety_checker/model.safetensors" # Replace with your .safetensors file path
|
11 |
+
model_data = safe_load(model_path)
|
12 |
|
13 |
+
# Specify the model name, adjust as necessary
|
14 |
+
model_name = "prompthero/openjourney-v4" # Replace with your model name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Load the tokenizer
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
|
|
18 |
|
19 |
+
# Load the model weights from safeload
|
20 |
+
model = AutoModel.from_pretrained(model_name, state_dict=model_data)
|
21 |
+
|
22 |
+
@app.route('/')
|
23 |
+
def index():
|
24 |
+
return "Welcome to the AI Model API!"
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
@app.route('/generate', methods=['POST'])
|
27 |
+
def generate_output():
|
28 |
data = request.json
|
29 |
+
prompt = data.get('prompt', 'Hello, world!')
|
30 |
|
31 |
+
# Tokenize input prompt
|
32 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
33 |
|
34 |
+
# Generate output
|
35 |
+
with torch.no_grad():
|
36 |
+
outputs = model(**inputs)
|
|
|
37 |
|
38 |
+
# Process and return the output
|
39 |
+
return jsonify(outputs)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
+
app.run(host='0.0.0.0', port=5000)
|
|