Spaces:
Build error

File size: 4,027 Bytes
7a30976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edb8575
7a30976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b9baad
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
import google.generativeai as genai
import gradio as gr
from ultralytics import YOLO

# 1. Google Generative AI API Anahtarını Ayarlama
genai.configure(api_key="AIzaSyCYXHPlZVZo1vUnV60UacVEr9wLKAWJ42k")

# 2. Google Generative AI Model Yapılandırması
generation_config = {
    "temperature": 1,
    "top_p": 0.95,
    "top_k": 40,
    "max_output_tokens": 8192,
    "response_mime_type": "text/plain",
}

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

# Sohbet oturumu başlatma
chat_session = model.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. 😊"
            ],
        }
    ]
)

# 3. YOLOv8-seg Modelini Yükleme
yolo_model = YOLO("best_dis.pt")  # Segmentation için eğitilmiş YOLOv8-seg model dosyanız

# Çürük tespiti ve chatbot entegrasyonu
def detect_and_respond(image, history):
    # 1. YOLOv8-seg ile Çürük Tespiti
    results = yolo_model(image)
    detected_image = results[0].plot()  # Segmentasyon maskeleri ile işaretlenmiş görüntü

    # Sınıf indekslerini doğru belirle
    caries_index = 0  # Caries (Erken çürük)
    cavity_index = 1  # Cavity (Çürük)

    # Her sınıf için sayım
    caries_count = 0
    cavity_count = 0

    for mask in results[0].boxes:
        class_id = int(mask.cls)  # Sınıf kimliği
        if class_id == caries_index:
            caries_count += 1
        elif class_id == cavity_index:
            cavity_count += 1

    # Tespit mesajını oluştur
    mesajlar = []
    if caries_count > 0:
        mesajlar.append(f"{caries_count} erken çürük tespit edildi.")
    if cavity_count > 0:
        mesajlar.append(f"{cavity_count} çürük tespit edildi.")
    if not mesajlar:
        mesajlar.append("Hiç çürük tespit edilmedi!")

    tespit_mesaji = " ".join(mesajlar)  # Mesajları birleştir

    # 2. Tespit Sonucunu Chatbot'a Gönderme
    chat_session.history.append({"role": "user", "parts": [tespit_mesaji]})
    response = chat_session.send_message(tespit_mesaji)
    chat_session.history.append({"role": "model", "parts": [response.text]})

    # 3. Gradio için tarihçeyi güncelleme
    history.append({"role": "user", "content": tespit_mesaji})
    history.append({"role": "assistant", "content": response.text})
    
    return detected_image, history

# Chatbot ile özgür konuşma
def chatbot_respond(user_input, history):
    chat_session.history.append({"role": "user", "parts": [user_input]})
    response = chat_session.send_message(user_input)
    chat_session.history.append({"role": "model", "parts": [response.text]})

    history.append({"role": "user", "content": user_input})
    history.append({"role": "assistant", "content": response.text})
    return history

# 4. Gradio Arayüzü
with gr.Blocks() as demo:
    gr.Markdown("## Çürük Tespiti ve Chatbot")

    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="pil", label="Diş 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")

    user_input = gr.Textbox(placeholder="Bir mesaj yazın ve Enter'a basın...")
    send_button = gr.Button("Gönder")

    detect_button.click(
        fn=detect_and_respond,
        inputs=[image_input, chatbot],
        outputs=[result_image, chatbot]
    )
    send_button.click(
        fn=chatbot_respond,
        inputs=[user_input, chatbot],
        outputs=chatbot
    )
    user_input.submit(
        fn=chatbot_respond,
        inputs=[user_input, chatbot],
        outputs=chatbot
    )

# 5. Uygulamayı Başlatma
if __name__ == "__main__":
    demo.launch(share=True)  # API'yi aktif hale getiriyoruz