Saad0KH commited on
Commit
91b5ad5
·
verified ·
1 Parent(s): b97a93e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -1,5 +1,4 @@
1
- from fastapi import FastAPI, Form, Response, UploadFile, File
2
- from fastapi.middleware.cors import CORSMiddleware
3
  from transformers import AutoProcessor, CLIPModel
4
  from PIL import Image
5
 
@@ -9,17 +8,8 @@ processor = AutoProcessor.from_pretrained("patrickjohncyh/fashion-clip")
9
 
10
 
11
  # Créer une instance FastAPI
12
- app = FastAPI()
13
 
14
- origins = ["*"]
15
-
16
- app.add_middleware(
17
- CORSMiddleware,
18
- allow_origins=origins,
19
- allow_credentials=True,
20
- allow_methods=["*"],
21
- allow_headers=["*"],
22
- )
23
 
24
  # Définir la fonction pour la classification d'image avec du texte en entrée
25
  def classify_image_with_text(text, image):
@@ -40,12 +30,15 @@ def classify_image_with_text(text, image):
40
  async def root():
41
  return {"message": "Welcome to the Fashion Clip API!"}
42
 
43
- # Définir une route d'API
44
- @app.post("/classify")
45
- async def classify(prompt: str, image: UploadFile = None):
46
- image_bytes = await image.read()
47
- image_pil = Image.open(io.BytesIO(image_bytes))
48
- return {"predicted_label": classify_image_with_text(prompt, image_pil)}
 
 
 
49
  if __name__ == '__main__':
50
  app.run(debug=True)
51
 
 
1
+ from flask import Flask, request, jsonify
 
2
  from transformers import AutoProcessor, CLIPModel
3
  from PIL import Image
4
 
 
8
 
9
 
10
  # Créer une instance FastAPI
11
+ app = Flask(__name__)
12
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Définir la fonction pour la classification d'image avec du texte en entrée
15
  def classify_image_with_text(text, image):
 
30
  async def root():
31
  return {"message": "Welcome to the Fashion Clip API!"}
32
 
33
+ # Route pour l'API REST
34
+ @app.route('/api/classify', methods=['POST'])
35
+ def classify():
36
+ data = request.json
37
+ text = data['text']
38
+ image = data['image']
39
+ result = classify_image_with_text(text, image)
40
+ return jsonify({'result': result})
41
+
42
  if __name__ == '__main__':
43
  app.run(debug=True)
44