Spaces:
Sleeping

File size: 4,415 Bytes
ea92c37
 
0461ca6
5ea840d
 
 
 
 
 
 
 
 
 
68dbbc6
7cdb281
5ea840d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cdb281
 
 
5ea840d
7cdb281
 
ea92c37
 
7cdb281
5ea840d
7cdb281
 
 
 
 
5ea840d
ea92c37
 
7cdb281
ea92c37
0461ca6
5ea840d
7cdb281
 
 
 
 
 
 
 
5ea840d
0461ca6
7cdb281
5ea840d
7cdb281
 
 
 
 
 
 
 
 
 
 
 
 
5ea840d
 
 
 
 
7cdb281
5ea840d
 
68dbbc6
ad86e00
5ea840d
ad86e00
 
 
 
5ea840d
 
 
 
 
ad86e00
5ea840d
 
68dbbc6
ad86e00
5ea840d
7cdb281
 
 
 
 
5ea840d
7cdb281
 
 
 
 
 
 
 
ad86e00
 
 
5ea840d
7cdb281
 
 
 
 
5ea840d
ad86e00
 
 
 
 
ea92c37
68dbbc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import gradio as gr
from ultralytics import YOLO
from PIL import Image, ImageDraw
import google.generativeai as genai

# 1. Google Generative AI API Ayarları
genai.configure(api_key="AIzaSyCYXHPlZVZo1vUnV60UacVEr9wLKAWJ42k")
generation_config = {
    "temperature": 1,
    "top_p": 0.95,
    "top_k": 40,
    "max_output_tokens": 8192,
    "response_mime_type": "text/plain",
}

model_gemini = genai.GenerativeModel(
    model_name="gemini-1.5-flash", generation_config=generation_config
)

chat_session = model_gemini.start_chat(
    history=[
        {
            "role": "model",
            "parts": [
                "Hoş geldiniz! Ben sanal bir diş hekimiyim. Çürük tespit sonuçlarını yorumlayabilir ve diş sağlığı hakkında bilgi verebilirim. 😊"
            ],
        }
    ]
)

# 2. YOLO Modelini Yükle
model = YOLO("best.pt")  # Çürük tespiti için eğitilmiş model dosyanız


# 3. Görüntü Üzerindeki Nesneleri Tespit Eden Fonksiyon
def detect_objects_on_image(image, history):
    results = model.predict(image)
    result = results[0]
    draw = ImageDraw.Draw(image)
    
    # Sınıf sayımı için değişkenler
    caries_count = 0
    deep_caries_count = 0
    impacted_count = 0
    lesion_count = 0
    
    for box in result.boxes:
        x1, y1, x2, y2 = [round(x) for x in box.xyxy[0].tolist()]
        class_id = int(box.cls[0].item())
        prob = round(box.conf[0].item(), 2)
        label = f"{result.names[class_id]}: {prob * 100:.2f}%"
        
        if result.names[class_id] == "Caries":
            caries_count += 1
        elif result.names[class_id] == "Deep Caries":
            deep_caries_count += 1
        elif result.names[class_id] == "Impacted":
            impacted_count += 1
        elif result.names[class_id] == "Periapical Lesion":
            lesion_count += 1
        
        draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
        draw.text((x1, y1 - 10), label, fill="red")
    
    tespit_mesaji = []
    if caries_count > 0:
        tespit_mesaji.append(f"{caries_count} erken çürük tespit edildi.")
    if deep_caries_count > 0:
        tespit_mesaji.append(f"{deep_caries_count} derin çürük tespit edildi.")
    if impacted_count > 0:
        tespit_mesaji.append(f"{impacted_count} gömülü diş tespit edildi.")
    if lesion_count > 0:
        tespit_mesaji.append(f"{lesion_count} periapikal lezyon tespit edildi.")
    if not tespit_mesaji:
        tespit_mesaji = ["Hiç sorun tespit edilmedi!"]

    tespit_mesaji_str = " ".join(tespit_mesaji)
    
    chat_session.history.append({"role": "user", "parts": [tespit_mesaji_str]})
    response = chat_session.send_message(tespit_mesaji_str)
    chat_session.history.append({"role": "model", "parts": [response.text]})
    
    history.append({"role": "user", "content": tespit_mesaji_str})
    history.append({"role": "assistant", "content": response.text})
    
    return image, history


# 4. Kullanıcı Mesajını İşleyen Fonksiyon
def send_user_message(user_message, history):
    if user_message.strip() == "":
        return history
    
    chat_session.history.append({"role": "user", "parts": [user_message]})
    response = chat_session.send_message(user_message)
    chat_session.history.append({"role": "model", "parts": [response.text]})
    
    history.append({"role": "user", "content": user_message})
    history.append({"role": "assistant", "content": response.text})
    
    return history


# 5. Gradio Arayüzü
interface = gr.Blocks()

with interface:
    gr.Markdown("## DİŞ FİLMLERİNDEN ÇÜRÜK TEŞHİSİ VE CHATBOT")
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="pil", label="Diş Röntgen Görüntüsü Yükleyin")
            detect_button = gr.Button("Tespit Et")
        with gr.Column():
            result_image = gr.Image(type="pil", label="Tespit Sonucu")
            chatbot = gr.Chatbot(label="Sanal Diş Hekimi Chatbot", type="messages")

    with gr.Row():
        user_input = gr.Textbox(label="Mesajınızı Yazın")
        send_button = gr.Button("Gönder")
    
    detect_button.click(
        fn=detect_objects_on_image,
        inputs=[image_input, chatbot],
        outputs=[result_image, chatbot]
    )
    
    send_button.click(
        fn=send_user_message,
        inputs=[user_input, chatbot],
        outputs=[chatbot]
    )

interface.launch()