Spaces:
Sleeping
Sleeping
Download app.py from unthinkablee/XRAY: direct link, hf CLI and curl.
- Browser
- Download file 4.42 kB
-
https://huggingface.co/spaces/unthinkablee/XRAY/resolve/main/app.py
- Command line
-
hf download hf://spaces/unthinkablee/XRAY/app.py
-
curl -L -o app.py https://huggingface.co/spaces/unthinkablee/XRAY/resolve/main/app.py
4.42 kB
| 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() |