keremsabirli commited on
Commit
aa296e9
1 Parent(s): 83e6a92

added custom pipeline

Browse files
Files changed (2) hide show
  1. app.py +31 -13
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,25 +1,43 @@
1
- from tkinter import Image
 
 
2
  import streamlit as st
3
- from transformers import pipeline
4
 
5
- model_name = "HuggingFaceM4/VLM_WebSight_finetuned"
6
- image_to_code = pipeline("image-to-text", model=model_name, trust_remote_code=True)
 
 
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  st.title("Image to Code Converter")
9
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
10
 
11
  if uploaded_file is not None:
12
- # To see details
13
  st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
14
  st.write("")
15
- st.write("Converting image to code...")
16
 
17
- # Convert the file to an image
18
- image = Image.open(uploaded_file)
19
-
20
- # Process the image
21
- code_result = image_to_code(image)
22
 
23
  # Display the code
24
- st.code(code_result[0]['generated_text'])
25
-
 
1
+ from PIL import Image as PILImage
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModelForCausalLM
4
  import streamlit as st
 
5
 
6
+ # Define your custom pipeline function
7
+ def custom_image_to_text_pipeline(image, processor, model, device):
8
+ # Preprocess the image
9
+ inputs = processor(images=image, return_tensors="pt")
10
+ inputs = {k: v.to(device) for k, v in inputs.items()}
11
 
12
+ # Generate predictions
13
+ output = model.generate(**inputs)
14
+
15
+ # Decode the output to text
16
+ decoded_output = processor.decode(output[0], skip_special_tokens=True)
17
+ return decoded_output
18
+
19
+ # Load your model and processor
20
+ device = torch.device("cuda")
21
+ processor = AutoProcessor.from_pretrained("HuggingFaceM4/VLM_WebSight_finetuned")
22
+ model = AutoModelForCausalLM.from_pretrained(
23
+ "HuggingFaceM4/VLM_WebSight_finetuned",
24
+ trust_remote_code=True,
25
+ torch_dtype=torch.bfloat16,
26
+ ).to(device)
27
+
28
+ # Streamlit UI
29
  st.title("Image to Code Converter")
30
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"])
31
 
32
  if uploaded_file is not None:
33
+ # Display the uploaded image
34
  st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
35
  st.write("")
 
36
 
37
+ # Convert the file to an image and process it
38
+ with PILImage.open(uploaded_file) as image:
39
+ st.write("Converting image to code...")
40
+ code_result = custom_image_to_text_pipeline(image, processor, model, device)
 
41
 
42
  # Display the code
43
+ st.code(code_result)
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  streamlit
2
  transformers
3
- torch
 
 
1
  streamlit
2
  transformers
3
+ torch
4
+ pillow