Omnibus commited on
Commit
06b2f35
1 Parent(s): e2346d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -3,6 +3,12 @@ import torch
3
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
4
  from numpy import exp
5
  import pandas as pd
 
 
 
 
 
 
6
 
7
  def softmax(vector):
8
  e = exp(vector)
@@ -15,7 +21,7 @@ models=[
15
  "arnolfokam/ai-generated-image-detector",
16
 
17
  ]
18
-
19
  def aiornot0(image):
20
  labels = ["Real", "AI"]
21
  mod=models[0]
@@ -31,8 +37,7 @@ def aiornot0(image):
31
  label = labels[prediction]
32
  html_out = f"""
33
  <h1>This image is likely: {label}</h1><br><h3>
34
- Model used: <a href='https://huggingface.co/{mod}'>{mod}</a><br>
35
- <br>
36
  Probabilites:<br>
37
  Real: {px[0][0]}<br>
38
  AI: {px[1][0]}"""
@@ -56,8 +61,7 @@ def aiornot1(image):
56
  label = labels[prediction]
57
  html_out = f"""
58
  <h1>This image is likely: {label}</h1><br><h3>
59
- Model used: <a href='https://huggingface.co/{mod}'>{mod}</a><br>
60
- <br>
61
  Probabilites:<br>
62
  Real: {px[0][0]}<br>
63
  AI: {px[1][0]}"""
@@ -81,8 +85,7 @@ def aiornot2(image):
81
  label = labels[prediction]
82
  html_out = f"""
83
  <h1>This image is likely: {label}</h1><br><h3>
84
- Model used: <a href='https://huggingface.co/{mod}'>{mod}</a><br>
85
- <br>
86
  Probabilites:<br>
87
  Real: {px[1][0]}<br>
88
  AI: {px[0][0]}"""
@@ -92,26 +95,45 @@ def aiornot2(image):
92
  results[labels[idx]] = px[idx][0]
93
  #results[labels['label']] = result['score']
94
  return gr.HTML.update(html_out),results
95
-
 
 
 
 
 
 
 
 
 
 
 
96
  with gr.Blocks() as app:
97
- with gr.Column():
 
 
 
 
 
 
98
  inp = gr.Pil()
99
- btn = gr.Button()
100
  with gr.Group():
101
  with gr.Row():
102
  with gr.Box():
103
- lab0 = gr.HTML(f"""<b>Testing on Model: {models[0]}</b>""")
104
- outp0 = gr.HTML("""""")
105
  n_out0=gr.Label(label="Output")
 
106
  with gr.Box():
107
- lab1 = gr.HTML(f"""<b>Testing on Model: {models[1]}</b>""")
108
- outp1 = gr.HTML("""""")
109
  n_out1=gr.Label(label="Output")
 
110
  with gr.Box():
111
- lab2 = gr.HTML(f"""<b>Testing on Model: {models[2]}</b>""")
112
- outp2 = gr.HTML("""""")
113
  n_out2=gr.Label(label="Output")
 
 
 
114
  btn.click(aiornot0,[inp],[outp0,n_out0])
115
  btn.click(aiornot1,[inp],[outp1,n_out1])
116
  btn.click(aiornot2,[inp],[outp2,n_out2])
117
- app.launch(enable_queue=False)
 
 
3
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
4
  from numpy import exp
5
  import pandas as pd
6
+ from PIL import Image
7
+ import urllib.request
8
+ import uuid
9
+ uid=uuid.uuid4()
10
+
11
+
12
 
13
  def softmax(vector):
14
  e = exp(vector)
 
21
  "arnolfokam/ai-generated-image-detector",
22
 
23
  ]
24
+
25
  def aiornot0(image):
26
  labels = ["Real", "AI"]
27
  mod=models[0]
 
37
  label = labels[prediction]
38
  html_out = f"""
39
  <h1>This image is likely: {label}</h1><br><h3>
40
+
 
41
  Probabilites:<br>
42
  Real: {px[0][0]}<br>
43
  AI: {px[1][0]}"""
 
61
  label = labels[prediction]
62
  html_out = f"""
63
  <h1>This image is likely: {label}</h1><br><h3>
64
+
 
65
  Probabilites:<br>
66
  Real: {px[0][0]}<br>
67
  AI: {px[1][0]}"""
 
85
  label = labels[prediction]
86
  html_out = f"""
87
  <h1>This image is likely: {label}</h1><br><h3>
88
+
 
89
  Probabilites:<br>
90
  Real: {px[1][0]}<br>
91
  AI: {px[0][0]}"""
 
95
  results[labels[idx]] = px[idx][0]
96
  #results[labels['label']] = result['score']
97
  return gr.HTML.update(html_out),results
98
+
99
+ def load_url(url):
100
+ try:
101
+ urllib.request.urlretrieve(
102
+ f'{url}',
103
+ f"{uid}tmp_im.png")
104
+ image = Image.open(f"{uid}tmp_im.png")
105
+ mes = "Image Loaded"
106
+ except Exception as e:
107
+ image=None
108
+ mes=f"Image not Found<br>Error: {e}"
109
+ return image,mes
110
  with gr.Blocks() as app:
111
+ with gr.Row():
112
+ with gr.Column():
113
+ in_url=gr.Textbox(label="Image URL")
114
+ with gr.Row():
115
+ load_btn=gr.Button("Load URL")
116
+ btn = gr.Button("Detect AI")
117
+ mes = gr.HTML("""""")
118
  inp = gr.Pil()
 
119
  with gr.Group():
120
  with gr.Row():
121
  with gr.Box():
122
+ lab0 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[0]}'>{models[0]}</a></b>""")
 
123
  n_out0=gr.Label(label="Output")
124
+ outp0 = gr.HTML("""""")
125
  with gr.Box():
126
+ lab1 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[1]}'>{models[1]}</a></b>""")
 
127
  n_out1=gr.Label(label="Output")
128
+ outp1 = gr.HTML("""""")
129
  with gr.Box():
130
+ lab2 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[2]}'>{models[2]}</a></b>""")
 
131
  n_out2=gr.Label(label="Output")
132
+ outp2 = gr.HTML("""""")
133
+
134
+ load_btn.click(load_url,in_url,[inp,mes])
135
  btn.click(aiornot0,[inp],[outp0,n_out0])
136
  btn.click(aiornot1,[inp],[outp1,n_out1])
137
  btn.click(aiornot2,[inp],[outp2,n_out2])
138
+
139
+ app.queue(concurrency_count=20).launch()