Ronith55 commited on
Commit
dd44da8
·
verified ·
1 Parent(s): 2182986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -1,32 +1,31 @@
1
- import gradio as gr
2
- from transformers import AutoModel, AutoTokenizer, AutoFeatureExtractor
3
  from PIL import Image
4
 
5
- # Load Deepseek-vl2-small model and tokenizer
6
- model_name = "deepseek-ai/deepseek-vl2-small" # Replace with actual model name if available on HF
7
- model = AutoModel.from_pretrained(model_name)
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
10
 
11
- # Define inference function
12
- def process_image_text(image, text):
13
- # Process inputs
14
- image_input = feature_extractor(images=image, return_tensors="pt")
15
- text_input = tokenizer(text, return_tensors="pt")
16
 
17
- # Get model output
18
- outputs = model(**text_input, **image_input)
 
 
 
 
19
 
20
- # Process output (modify based on your model’s task)
21
- return "Model processed the inputs successfully!"
 
 
 
 
 
22
 
23
- # Create Gradio interface
24
- interface = gr.Interface(
25
- fn=process_image_text,
26
- inputs=[gr.Image(type="pil"), gr.Textbox()],
27
- outputs="text",
28
- title="deepseek-vl2-small Demo"
29
- )
30
 
31
- # Launch app
32
- interface.launch()
 
1
+ import torch
2
+ from transformers import AutoProcessor, AutoModelForVision2Seq
3
  from PIL import Image
4
 
5
+ # Define the model name from Hugging Face
6
+ MODEL_NAME = "deepseek-ai/deepseek-vl2-small"
 
 
 
7
 
8
+ # Load model and processor
9
+ processor = AutoProcessor.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForVision2Seq.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
 
 
11
 
12
+ # Test the model with an image
13
+ def predict(image_path):
14
+ image = Image.open(image_path).convert("RGB")
15
+
16
+ # Process input
17
+ inputs = processor(images=image, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
18
 
19
+ # Generate output
20
+ output = model.generate(**inputs)
21
+
22
+ # Decode response
23
+ generated_text = processor.batch_decode(output, skip_special_tokens=True)[0]
24
+
25
+ return generated_text
26
 
27
+ # Example Usage
28
+ if __name__ == "__main__":
29
+ test_image_path = "test.jpg" # Replace with an actual image path
30
+ print("Generated Output:", predict(test_image_path))
 
 
 
31