byh711 commited on
Commit
85aeaf9
1 Parent(s): 6929548

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -127
app.py CHANGED
@@ -1,142 +1,149 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- #import spaces #[uncomment to use ZeroGPU]
5
- from diffusers import DiffusionPipeline
6
  import torch
 
 
7
 
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
 
11
- if torch.cuda.is_available():
12
- torch_dtype = torch.float16
13
- else:
14
- torch_dtype = torch.float32
15
 
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
- pipe = pipe.to(device)
 
18
 
19
- MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
 
21
 
22
- #@spaces.GPU #[uncomment to use ZeroGPU]
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- if randomize_seed:
26
- seed = random.randint(0, MAX_SEED)
27
-
28
- generator = torch.Generator().manual_seed(seed)
29
-
30
- image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- return image, seed
41
-
42
- examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
- ]
47
-
48
- css="""
49
- #col-container {
50
- margin: 0 auto;
51
- max-width: 640px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
53
  """
54
 
55
- with gr.Blocks(css=css) as demo:
56
-
57
- with gr.Column(elem_id="col-container"):
58
- gr.Markdown(f"""
59
- # Text-to-Image Gradio Template
60
- """)
61
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  with gr.Row():
63
-
64
- prompt = gr.Text(
65
- label="Prompt",
66
- show_label=False,
67
- max_lines=1,
68
- placeholder="Enter your prompt",
69
- container=False,
70
- )
71
-
72
- run_button = gr.Button("Run", scale=0)
73
-
74
- result = gr.Image(label="Result", show_label=False)
75
-
76
- with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
- max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
- )
84
-
85
- seed = gr.Slider(
86
- label="Seed",
87
- minimum=0,
88
- maximum=MAX_SEED,
89
- step=1,
90
- value=0,
91
- )
92
-
93
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
- with gr.Row():
96
-
97
- width = gr.Slider(
98
- label="Width",
99
- minimum=256,
100
- maximum=MAX_IMAGE_SIZE,
101
- step=32,
102
- value=1024, #Replace with defaults that work for your model
103
- )
104
-
105
- height = gr.Slider(
106
- label="Height",
107
- minimum=256,
108
- maximum=MAX_IMAGE_SIZE,
109
- step=32,
110
- value=1024, #Replace with defaults that work for your model
111
- )
112
-
113
- with gr.Row():
114
-
115
- guidance_scale = gr.Slider(
116
- label="Guidance scale",
117
- minimum=0.0,
118
- maximum=10.0,
119
- step=0.1,
120
- value=0.0, #Replace with defaults that work for your model
121
- )
122
-
123
- num_inference_steps = gr.Slider(
124
- label="Number of inference steps",
125
- minimum=1,
126
- maximum=50,
127
- step=1,
128
- value=2, #Replace with defaults that work for your model
129
- )
130
-
131
- gr.Examples(
132
- examples = examples,
133
- inputs = [prompt]
134
  )
135
- gr.on(
136
- triggers=[run_button.click, prompt.submit],
137
- fn = infer,
138
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs = [result, seed]
140
- )
141
 
142
- demo.queue().launch()
 
1
  import gradio as gr
2
+ from transformers import AutoProcessor, AutoModelForCausalLM
3
+ from PIL import Image
 
 
4
  import torch
5
+ from peft import PeftModel
6
+ import numpy as np
7
 
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ torch_dtype = torch.float32
10
 
11
+ # Load the fine-tuned base model
12
+ base_model = AutoModelForCausalLM.from_pretrained('byh711/FLODA-deepfake', trust_remote_code=True, torch_dtype=torch_dtype).to(device)
13
+ processor = AutoProcessor.from_pretrained('byh711/FLODA-deepfake', trust_remote_code=True)
 
14
 
15
+ # Load the LoRA weights
16
+ model = PeftModel.from_pretrained(base_model, peft_model_path)
17
+ model.eval()
18
 
19
+ def caption_generate(task_prompt, text_input=None, image=None):
20
+ if isinstance(image, np.ndarray):
21
+ image = Image.fromarray(image)
22
 
23
+ if text_input is None:
24
+ prompt = task_prompt
25
+ else:
26
+ prompt = task_prompt + text_input
27
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
28
+ generated_ids = model.generate(
29
+ input_ids=inputs["input_ids"],
30
+ pixel_values=inputs["pixel_values"],
31
+ max_new_tokens=1024,
32
+ num_beams=3
33
+ )
34
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
35
+ parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
36
 
37
+ return parsed_answer[task_prompt][1:-1]
38
+
39
+
40
+ def run_example(task_prompt, text_input=None, image=None):
41
+
42
+ if text_input is None:
43
+ prompt = task_prompt
44
+ else:
45
+ prompt = task_prompt + text_input
46
+
47
+ if isinstance(image, np.ndarray):
48
+ image = Image.fromarray(image)
49
+
50
+ image = image.convert("RGB")
51
+
52
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
53
+ inputs = {k: v.to(torch_dtype) if v.is_floating_point() else v for k, v in inputs.items()}
54
+
55
+ generated_ids = base_model.generate(
56
+ input_ids=inputs["input_ids"],
57
+ pixel_values=inputs["pixel_values"],
58
+ max_new_tokens=1024,
59
+ num_beams=3
60
+ )
61
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
62
+ result = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))['<DEEPFAKE_DETECTION>']
63
 
64
+ if result.lower() == "yes":
65
+ return "This is a real image."
66
+ elif result.lower() == "no":
67
+ return "This is a fake image."
68
+ else:
69
+ return f"Uncertain. Model output: {result}"
70
+
71
+ # Define the Gradio interface
72
+ css = """
73
+ body {
74
+ background-color: #1e1e2e;
75
+ color: #d4d4dc;
76
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
77
+ }
78
+
79
+ #output {
80
+ height: 500px;
81
+ overflow: auto;
82
+ border: 1px solid #444;
83
+ background-color: #282c34;
84
+ color: #f1f1f1;
85
+ padding: 10px;
86
+ }
87
+
88
+ .gr-button {
89
+ background-color: #3a3f51;
90
+ border: none;
91
+ color: #ffffff;
92
+ padding: 10px 20px;
93
+ text-align: center;
94
+ font-size: 14px;
95
+ cursor: pointer;
96
+ transition: 0.3s;
97
+ }
98
+
99
+ .gr-button:hover {
100
+ background-color: #4b5263;
101
+ }
102
+
103
+ .gr-textbox {
104
+ background-color: #2e2e38;
105
+ border: 1px solid #555;
106
+ color: #ffffff;
107
+ }
108
+
109
+ .gr-markdown {
110
+ color: #d4d4dc;
111
  }
112
  """
113
 
114
+ js_func = """
115
+ function refresh() {
116
+ const url = new URL(window.location);
117
+
118
+ if (url.searchParams.get('__theme') !== 'dark') {
119
+ url.searchParams.set('__theme', 'dark');
120
+ window.location.href = url.href;
121
+ }
122
+ }
123
+ """
124
+
125
+ TITLE = "# FLODA: Vision-Language Models for Deepfake Detection"
126
+ DESCRIPTION = """
127
+ FLODA (FLorence-2 Optimized for Deepfake Assessment) is an advanced deepfake detection model leveraging the power of [Florence-2](https://huggingface.co/microsoft/Florence-2-base-ft).
128
+ FLODA combines image captioning with authenticity assessment in a single end-to-end architecture, demonstrating superior performance compared to existing benchmarks.
129
+ Learn more about FLODA in the published paper [here](https://github.com/byh711/FLODA).
130
+ """
131
+
132
+ with gr.Blocks(js=js_func, css=css) as demo:
133
+ gr.Markdown(TITLE)
134
+ gr.Markdown(DESCRIPTION)
135
+ with gr.Tab(label="FLODA: Deepfake Detection"):
136
  with gr.Row():
137
+ with gr.Column():
138
+ input_img = gr.Image(label="Input Picture", type="numpy")
139
+ submit_btn = gr.Button(value="Submit")
140
+ with gr.Column():
141
+ output_text = gr.Textbox(label="Output Text")
142
+
143
+ submit_btn.click(
144
+ fn=lambda image: run_example("<DEEPFAKE_DETECTION>", text_input=None, image=image),
145
+ inputs=[input_img],
146
+ outputs=[output_text]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  )
 
 
 
 
 
 
148
 
149
+ demo.launch(debug=True)