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