Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,33 +3,92 @@ from ultralytics import YOLO
|
|
3 |
from PIL import Image
|
4 |
from ultralytics.utils.plotting import Annotator, colors
|
5 |
import glob
|
|
|
|
|
|
|
|
|
6 |
|
|
|
7 |
# Load model and data
|
8 |
model = YOLO('Dental_model.pt')
|
9 |
pic_files = glob.glob('*.jpg')
|
10 |
names = model.model.names
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
image1 = image.copy()
|
14 |
-
|
15 |
-
classes =
|
16 |
-
boxes =
|
17 |
annotator = Annotator(image, line_width=3)
|
18 |
annotator1 = Annotator(image1, line_width=3)
|
19 |
-
|
20 |
for box, cls in zip(boxes, classes):
|
21 |
annotator.box_label(box, label=names[int(cls)], color=colors(int(cls)))
|
22 |
annotator1.box_label(box, label=None, color=colors(int(cls)))
|
23 |
|
24 |
-
return Image.fromarray(annotator.result()), Image.fromarray(annotator1.result())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Gradio Blocks App
|
27 |
with gr.Blocks() as demo:
|
28 |
gr.Markdown("## Dental Analysis")
|
29 |
gr.Markdown("Analyze your Dental XRAY image with our AI object Detection model")
|
|
|
30 |
|
31 |
with gr.Row():
|
32 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
image_input = gr.Image(type="pil", label="Upload Image")
|
34 |
run_button = gr.Button("Run Detection")
|
35 |
example_images = gr.Examples(
|
@@ -40,10 +99,7 @@ with gr.Blocks() as demo:
|
|
40 |
with gr.Column():
|
41 |
image_output_1 = gr.Image(type="pil", label="Dental Analysis")
|
42 |
image_output_2 = gr.Image(type="pil", label="Without Labels")
|
43 |
-
|
44 |
-
run_button.click(fn=detect_objects,
|
45 |
-
inputs=image_input,
|
46 |
-
outputs=[image_output_1, image_output_2])
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
demo.launch()
|
|
|
3 |
from PIL import Image
|
4 |
from ultralytics.utils.plotting import Annotator, colors
|
5 |
import glob
|
6 |
+
from database_center import db_transaction
|
7 |
+
from cloudhands import CloudHandsPayment
|
8 |
+
import uuid
|
9 |
+
import os
|
10 |
|
11 |
+
payment_key=os.getenv('Payment_Key')
|
12 |
# Load model and data
|
13 |
model = YOLO('Dental_model.pt')
|
14 |
pic_files = glob.glob('*.jpg')
|
15 |
names = model.model.names
|
16 |
|
17 |
+
# ...existing code...
|
18 |
+
def detect_objects(state, image):
|
19 |
+
chpay = state['chpay']
|
20 |
+
payment_result = None
|
21 |
+
|
22 |
+
# attempt to charge only if we have a token
|
23 |
+
if state.get('token'):
|
24 |
+
try:
|
25 |
+
payment_result = chpay.charge(charge=0.2)
|
26 |
+
except Exception as e:
|
27 |
+
print(f"Error charging payment: {e}")
|
28 |
+
payment_result = None
|
29 |
+
else:
|
30 |
+
print("No payment token. Please authorize first.")
|
31 |
+
|
32 |
+
# if no successful payment, return empty outputs (Gradio expects two outputs)
|
33 |
+
if not getattr(payment_result, "transaction_id", None):
|
34 |
+
gr.Info('Transaction Failed')
|
35 |
+
return None, None,
|
36 |
+
|
37 |
+
if image is None:
|
38 |
+
return None, None
|
39 |
+
gr.Info('Transaction completed')
|
40 |
+
db_transaction.add({
|
41 |
+
'id':str(uuid.uuid4()),
|
42 |
+
'app':'Dental Analysis',
|
43 |
+
'transaction-id':payment_result.transaction_id,
|
44 |
+
'price':0.1
|
45 |
+
|
46 |
+
})
|
47 |
image1 = image.copy()
|
48 |
+
detection_results = model.predict(image)
|
49 |
+
classes = detection_results[0].boxes.cls.cpu().tolist()
|
50 |
+
boxes = detection_results[0].boxes.xyxy.cpu()
|
51 |
annotator = Annotator(image, line_width=3)
|
52 |
annotator1 = Annotator(image1, line_width=3)
|
53 |
+
|
54 |
for box, cls in zip(boxes, classes):
|
55 |
annotator.box_label(box, label=names[int(cls)], color=colors(int(cls)))
|
56 |
annotator1.box_label(box, label=None, color=colors(int(cls)))
|
57 |
|
58 |
+
return Image.fromarray(annotator.result()), Image.fromarray(annotator1.result()),'Payment succeed'
|
59 |
+
# ...existing code...
|
60 |
+
|
61 |
+
# ...existing code...
|
62 |
+
def Authorise_link(state):
|
63 |
+
chpay = state['chpay']
|
64 |
+
url = chpay.get_authorization_url()
|
65 |
+
# return an HTML anchor and attempt to open a new tab (browser may block auto-open)
|
66 |
+
html = f'<a href="{url}" target="_blank" rel="noopener noreferrer">Open authorization in a new tab</a>'
|
67 |
+
html += f'<script>window.open("{url}", "_blank");</script>'
|
68 |
+
return html
|
69 |
+
# ...existing code...
|
70 |
+
|
71 |
+
def set_api_key(state, payment_api_key):
|
72 |
+
chpay = state['chpay']
|
73 |
+
token = chpay.exchange_code_for_token(payment_api_key)
|
74 |
+
state['token'] = token
|
75 |
+
return state
|
76 |
|
77 |
# Gradio Blocks App
|
78 |
with gr.Blocks() as demo:
|
79 |
gr.Markdown("## Dental Analysis")
|
80 |
gr.Markdown("Analyze your Dental XRAY image with our AI object Detection model")
|
81 |
+
payment_state = gr.State({'chpay':CloudHandsPayment(author_key=payment_key), 'token':None, 'transaction_id':None})
|
82 |
|
83 |
with gr.Row():
|
84 |
with gr.Column():
|
85 |
+
apiKey = gr.Button("Get_api_key")
|
86 |
+
# add an HTML output to receive the returned link/script
|
87 |
+
auth_link = gr.HTML("")
|
88 |
+
apiKey.click(fn=Authorise_link, inputs=payment_state, outputs=auth_link)
|
89 |
+
payment_api_key=gr.Textbox(label="Authorization token", interactive=True)
|
90 |
+
authorise_button=gr.Button("Authorise Payment")
|
91 |
+
authorise_button.click(fn=set_api_key, inputs=(payment_state, payment_api_key), outputs=payment_state)
|
92 |
image_input = gr.Image(type="pil", label="Upload Image")
|
93 |
run_button = gr.Button("Run Detection")
|
94 |
example_images = gr.Examples(
|
|
|
99 |
with gr.Column():
|
100 |
image_output_1 = gr.Image(type="pil", label="Dental Analysis")
|
101 |
image_output_2 = gr.Image(type="pil", label="Without Labels")
|
102 |
+
run_button.click(detect_objects,inputs=[payment_state,image_input],outputs=[image_output_1,image_output_2])
|
|
|
|
|
|
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
demo.launch()
|