Shahriar-jaman commited on
Commit
792265e
·
verified ·
1 Parent(s): eeb9acb

Update Vision-proxy/app.py

Browse files
Files changed (1) hide show
  1. Vision-proxy/app.py +25 -47
Vision-proxy/app.py CHANGED
@@ -1,47 +1,25 @@
1
- import torch
2
- from PIL import Image
3
- from transformers import AutoProcessor, AutoModelForVision2Seq
4
- import gradio as gr
5
- import os
6
-
7
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
8
-
9
- token = os.environ.get("HF_TOKEN")
10
-
11
- processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-Instruct", token=token)
12
- model = AutoModelForVision2Seq.from_pretrained(
13
- "HuggingFaceTB/SmolVLM-Instruct",
14
- torch_dtype=torch.bfloat16 if DEVICE == "cuda" else torch.float32,
15
- _attn_implementation="flash_attention_2" if DEVICE == "cuda" else "eager",
16
- token=token
17
- ).to(DEVICE)
18
-
19
- def describe_image(image):
20
- messages = [
21
- {
22
- "role": "user",
23
- "content": [
24
- {"type": "image"},
25
- {"type": "text", "text": "Describe this image in detail."}
26
- ]
27
- },
28
- ]
29
-
30
- prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
31
- inputs = processor(text=prompt, images=[image], return_tensors="pt").to(DEVICE)
32
-
33
- with torch.no_grad():
34
- generated_ids = model.generate(**inputs, max_new_tokens=500)
35
-
36
- result = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
37
- return result
38
-
39
- demo = gr.Interface(
40
- fn=describe_image,
41
- inputs=gr.Image(type="pil", label="Upload an Image"),
42
- outputs="text",
43
- title="VISIONSAGE",
44
- description="Upload an image and get a detailed description."
45
- )
46
-
47
- demo.launch()
 
1
+ from flask import Flask, request, jsonify, send_from_directory
2
+ from flask_cors import CORS
3
+ import requests
4
+
5
+ app = Flask(__name__, static_folder='static')
6
+ CORS(app) # allow all origins
7
+
8
+ HF_API_URL = "https://hf.space/embed/HawkEye01/VisionSage/api/predict/"
9
+
10
+ @app.route('/api/predict', methods=['POST'])
11
+ def predict():
12
+ try:
13
+ data = request.get_json()
14
+ response = requests.post(HF_API_URL, json=data, headers={"Content-Type": "application/json"})
15
+ return jsonify(response.json())
16
+ except Exception as e:
17
+ return jsonify({"error": str(e)}), 500
18
+
19
+ # Serve HTML
20
+ @app.route('/')
21
+ def index():
22
+ return send_from_directory('static', 'index.html')
23
+
24
+ if __name__ == '__main__':
25
+ app.run(debug=True)