far555na commited on
Commit
2434931
·
verified ·
1 Parent(s): 402d19b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py CHANGED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoProcessor, AutoModelForCausalLM, AutoConfig
2
+ import gradio as gr
3
+ import torch
4
+
5
+ # # Load the processor and model
6
+ processor = AutoProcessor.from_pretrained("microsoft/git-base")
7
+ # config = AutoConfig.from_pretrained("./adapter_config.json")
8
+ # # model = AutoModelForCausalLM.from_pretrained("microsoft/git-base")
9
+
10
+ # model_path = "./adapter_model.safetensors"
11
+ # model = AutoModelForCausalLM.from_pretrained(model_path)
12
+
13
+ from transformers import AutoModelForCausalLM
14
+ from peft import PeftModel
15
+
16
+ #Base model on your local filesystem
17
+ base_model_dir = "microsoft/git-base"
18
+ base_model = AutoModelForCausalLM.from_pretrained(base_model_dir)
19
+
20
+ #Adaptor directory on your local filesystem
21
+ adaptor_dir = "./"
22
+ merged_model = PeftModel.from_pretrained(base_model,adaptor_dir)
23
+
24
+ merged_model = merged_model.merge_and_unload()
25
+ merged_model.save_pretrained("./Merged-Model/")
26
+
27
+ model = merged_model
28
+
29
+
30
+ def predict(image):
31
+ try:
32
+ # Prepare the image using the processor
33
+ inputs = processor(images=image, return_tensors="pt")
34
+
35
+ # Move inputs to the appropriate device
36
+ device = "cuda" if torch.cuda.is_available() else "cpu"
37
+ inputs = {key: value.to(device) for key, value in inputs.items()}
38
+ model.to(device)
39
+
40
+ # Generate the caption
41
+ outputs = model.generate(**inputs)
42
+
43
+ # Decode the generated caption
44
+ caption = processor.batch_decode(outputs, skip_special_tokens=True)[0]
45
+
46
+ return caption
47
+
48
+ except Exception as e:
49
+ print("Error during prediction:", str(e))
50
+ return "Error: " + str(e)
51
+
52
+ # https://www.gradio.app/guides
53
+ with gr.Blocks() as demo:
54
+ image = gr.Image(type="pil")
55
+ predict_btn = gr.Button("Predict", variant="primary")
56
+ output = gr.Label(label="Generated Caption")
57
+
58
+ inputs = [image]
59
+ outputs = [output]
60
+
61
+ predict_btn.click(predict, inputs=inputs, outputs=outputs)
62
+
63
+ if __name__ == "__main__":
64
+ demo.launch() # Local machine only
65
+ # demo.launch(server_name="0.0.0.0") # LAN access to local machine
66
+ # demo.launch(share=True) # Public access to local machine
67
+