Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,63 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for
|
2 |
+
import requests
|
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')
|
16 |
+
|
17 |
+
@app.route('/generate', methods=['POST'])
|
18 |
+
def generate():
|
19 |
+
image = request.files['image']
|
20 |
+
mood = get_mood_from_image(image)
|
21 |
+
|
22 |
+
if mood:
|
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 |
+
def get_mood_from_image(image):
|
29 |
+
# Implement mood classification logic using the CLIP API
|
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)
|