Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,10 @@
|
|
1 |
from flask import Flask, render_template, request, redirect, url_for
|
2 |
-
import
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
-
import base64
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
-
# Replace with your own API keys
|
10 |
-
CLIP_API_KEY = "your_clip_api_key"
|
11 |
-
STABLE_DIFFUSION_API_KEY = "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"
|
12 |
-
|
13 |
@app.route('/')
|
14 |
def index():
|
15 |
return render_template('index.html')
|
@@ -17,47 +12,15 @@ def index():
|
|
17 |
@app.route('/generate', methods=['POST'])
|
18 |
def generate():
|
19 |
image = request.files['image']
|
20 |
-
|
21 |
-
|
22 |
-
if
|
23 |
-
art, narrative = generate_art_and_narrative(mood)
|
24 |
return render_template('result.html', art=art, narrative=narrative)
|
25 |
else:
|
26 |
return redirect(url_for('index'))
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
moods = ["happy", "sad", "angry", "neutral"]
|
31 |
-
prompt = "The mood of the person in this image is: "
|
32 |
-
|
33 |
-
headers = {
|
34 |
-
"Authorization": f"Bearer {CLIP_API_KEY}"
|
35 |
-
}
|
36 |
-
|
37 |
-
# Convert the image to base64
|
38 |
-
image_base64 = base64.b64encode(image.read()).decode('utf-8')
|
39 |
-
|
40 |
-
json_data = {
|
41 |
-
"inputs": [{"data": {"image": {"base64": image_base64}}, "prompt": prompt} for mood in moods]
|
42 |
-
}
|
43 |
-
|
44 |
-
response = requests.post('https://api-inference.huggingface.co/models/openai/clip-vit-base-patch32', headers=headers, json=json_data).json()
|
45 |
-
|
46 |
-
mood_scores = {}
|
47 |
-
for choice, mood in zip(response, moods):
|
48 |
-
mood_scores[mood] = float(choice['scores'][0])
|
49 |
-
|
50 |
-
# Filter moods with a score above 60%
|
51 |
-
filtered_moods = {k: v for k, v in mood_scores.items() if v > 0.6}
|
52 |
-
|
53 |
-
if len(filtered_moods) < 2:
|
54 |
-
return None
|
55 |
-
|
56 |
-
return filtered_moods
|
57 |
-
|
58 |
-
def generate_art_and_narrative(mood):
|
59 |
-
# Implement art generation logic using the Stable Diffusion API
|
60 |
-
pass
|
61 |
|
62 |
if __name__ == '__main__':
|
63 |
app.run(debug=True)
|
|
|
1 |
from flask import Flask, render_template, request, redirect, url_for
|
2 |
+
from generate import mood_art_generator
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
|
|
|
|
|
|
|
|
8 |
@app.route('/')
|
9 |
def index():
|
10 |
return render_template('index.html')
|
|
|
12 |
@app.route('/generate', methods=['POST'])
|
13 |
def generate():
|
14 |
image = request.files['image']
|
15 |
+
image = Image.open(BytesIO(image.read()))
|
16 |
+
art, narrative = mood_art_generator(image)
|
17 |
+
if art:
|
|
|
18 |
return render_template('result.html', art=art, narrative=narrative)
|
19 |
else:
|
20 |
return redirect(url_for('index'))
|
21 |
|
22 |
+
if __name__ == '__main__':
|
23 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
if __name__ == '__main__':
|
26 |
app.run(debug=True)
|