import gradio as gr import cv2 import numpy as np import requests import g4f import time import os theme = gr.themes.Base( primary_hue="cyan", secondary_hue="blue", neutral_hue="slate", ) API_KEY = os.environ.get("API_KEY") BRAIN_TUMOR_API_URL = "https://api-inference.huggingface.co/models/Devarshi/Brain_Tumor_Classification" BREAST_CANCER_API_URL = "https://api-inference.huggingface.co/models/MUmairAB/Breast_Cancer_Detector" ALZHEIMER_API_URL = "https://api-inference.huggingface.co/models/AhmadHakami/alzheimer-image-classification-google-vit-base-patch16" headers = {"Authorization": "Bearer "+ API_KEY+"", 'Content-Type': 'application/json'} # Create a function to Detect/Classify Alzheimer def classify_alzheimer(image): image_data = np.array(image, dtype=np.uint8) _, buffer = cv2.imencode('.jpg', image_data) binary_data = buffer.tobytes() response = requests.post(ALZHEIMER_API_URL, headers=headers, data=binary_data) result = {item['label']: item['score'] for item in response.json()} return result # Create a function to Detect/Classify Breast Cancer def classify_breast_cancer(image): image_data = np.array(image, dtype=np.uint8) _, buffer = cv2.imencode('.jpg', image_data) binary_data = buffer.tobytes() response = requests.post(BREAST_CANCER_API_URL, headers=headers, data=binary_data) result = {item['label']: item['score'] for item in response.json()} return result # Create a function to Detect/Classify Brain Tumor def classify_brain_tumor(image): image_data = np.array(image, dtype=np.uint8) _, buffer = cv2.imencode('.jpg', image_data) binary_data = buffer.tobytes() response = requests.post(BRAIN_TUMOR_API_URL, headers=headers, data=binary_data) result = {item['label']: item['score'] for item in response.json()} return result # Create the Gradio interface with gr.Blocks(theme=theme) as Alzheimer: with gr.Row(): with gr.Column(): gr.Markdown("# Alzheimer Detection and Classification") gr.Markdown("> Classify the alzheimer into Mild Demented, Very Mild Demented, Moderate Demented and Non Demented.") image = gr.Image() output = gr.Label(label='Alzheimer Classification', container=True, scale=2) with gr.Row(): button = gr.Button(value="Submit", variant="primary") gr.ClearButton([image, output]) button.click(classify_alzheimer, [image], [output]) def respond(message, history): bot_message = g4f.ChatCompletion.create( model="gpt-3.5-turbo", provider=g4f.Provider.GptGo, messages=[{"role": "user", "content": "Your role is Alzheimer Disease Expert. Now I will provide you with the user query. First check if the user query is related to Alzheimer or not. If it is not related to Alzheimer then do not reply the query whereas if related to Alzheimer reply it as usual. Here's the user Query:" + message}], ) time.sleep(1) yield str(bot_message) with gr.Column(): gr.Markdown("# Health Bot for Alzheimer") gr.Markdown("> **Note:** The information may not be accurate. Please consult a Doctor before considering any actions.") gr.ChatInterface(respond, autofocus=False).queue() with gr.Blocks(theme=theme) as BreastCancer: with gr.Row(): with gr.Column(): gr.Markdown("# Breast Cancer Detection and Classification") gr.Markdown("> Classify the breast cancer.") image = gr.Image() output = gr.Label(label='Breast Cancer Classification', container=True, scale=2) with gr.Row(): button = gr.Button(value="Classify") gr.ClearButton([image, output]) button.click(classify_breast_cancer, [image], [output]) def respond(message, history): bot_message = g4f.ChatCompletion.create( model="gpt-3.5-turbo", provider=g4f.Provider.GptGo, messages=[{"role": "user", "content": "Your role is Breast Cancer Disease Expert. Now I will provide you with the user query. First check if the user query is related to Breast Cancer or not. If it is not related to Breast Cancer then do not reply the query whereas if related to Breast Cancer reply it as usual. Here's the user Query:" + message}], ) time.sleep(1) yield str(bot_message) with gr.Column(): gr.Markdown("# Health Bot for Breast Cancer") gr.Markdown("> **Note:** The information may not be accurate. Please consult a Doctor before considering any actions.") gr.ChatInterface(respond, autofocus=False).queue() with gr.Blocks(theme=theme) as BrainTumor: with gr.Row(): with gr.Column(): gr.Markdown("# Brain Tumor Detection and Classification") gr.Markdown("> Classify the Brain Tumor.") image = gr.Image() output = gr.Label(label='Brain Tumor Classification', container=True, scale=2) with gr.Row(): button = gr.Button(value="Classify") gr.ClearButton([image, output]) button.click(classify_brain_tumor, [image], [output]) def respond(message, history): bot_message = g4f.ChatCompletion.create( model="gpt-3.5-turbo", provider=g4f.Provider.GptGo, messages=[{"role": "user", "content": "Your role is Brain Tumor Disease Expert. Now I will provide you with the user query. First check if the user query is related to Brain Tumor or not. If it is not related to Brain Tumor then do not reply the query whereas if related to Brain Tumor reply it as usual. Here's the user Query:" + message}], ) time.sleep(1) yield str(bot_message) with gr.Column(): gr.Markdown("# Health Bot for Brain Tumor") gr.Markdown("> **Note:** The information may not be accurate. Please consult a Doctor before considering any actions.") gr.ChatInterface(respond, autofocus=False, examples=["Explain Brain Tumor."]).queue() Main = gr.TabbedInterface([Alzheimer, BreastCancer, BrainTumor], ["Alzheimer", "Breast Cancer", "Brain Tumor"], theme=theme, css=".gradio-container { background: rgba(255, 255, 255, 0.2) !important; box-shadow: 0 8px 32px 0 rgba( 31, 38, 135, 0.37 ) !important !important; backdrop-filter: blur( 10px ) !important; -webkit-backdrop-filter: blur( 10px ) !important; border-radius: 10px !important; border: 1px solid rgba( 255, 255, 255, 0.18 ) !important;}") Main.launch()