Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,79 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from transformers import AutoProcessor,
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
import tempfile
|
| 7 |
from PIL import Image
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class AIDoctor:
|
| 10 |
-
def __init__(self,
|
| 11 |
self.device = "cpu"
|
| 12 |
-
print(f"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
trust_remote_code=True
|
| 18 |
).to(self.device)
|
|
|
|
|
|
|
| 19 |
self.stt = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
|
| 20 |
-
|
| 21 |
def analyze(self, image, question):
|
| 22 |
if image is None:
|
| 23 |
-
return "Please upload a medical image."
|
| 24 |
-
prompt = question or "
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
def tts(self, text):
|
| 30 |
tts = gTTS(text=text, lang="en")
|
| 31 |
-
|
| 32 |
-
tts.save(
|
| 33 |
-
return
|
| 34 |
-
|
| 35 |
def respond(self, image, audio, text):
|
| 36 |
-
|
| 37 |
if audio:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if
|
| 41 |
-
|
| 42 |
-
resp = self.analyze(image,
|
| 43 |
voice = self.tts(resp)
|
| 44 |
-
return resp, voice,
|
| 45 |
|
|
|
|
| 46 |
doctor = AIDoctor()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
with gr.Row():
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
resp_out = gr.Textbox(label="AI Response", lines=10)
|
| 55 |
aud_out = gr.Audio(label="AI Speaks", type="filepath")
|
| 56 |
-
q_out = gr.Textbox(label="Processed Question")
|
| 57 |
-
|
| 58 |
-
btn.
|
|
|
|
|
|
|
| 59 |
outputs=[resp_out, aud_out, q_out])
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoProcessor, MllamaForConditionalGeneration, pipeline
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
from gtts import gTTS
|
| 7 |
import tempfile
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
+
# ββββββββββββββββ
|
| 11 |
+
# π‘ STEP 0: AUTHENTICATE WITH HF
|
| 12 |
+
login(token=os.getenv("HUGGINGFACE_TOKEN")) # Or paste your token: "hf_xxx"
|
| 13 |
+
|
| 14 |
+
# ββββββββββββββββ
|
| 15 |
class AIDoctor:
|
| 16 |
+
def __init__(self, model_id="meta-llama/Llama-3.2-11B-Vision-Instruct"):
|
| 17 |
self.device = "cpu"
|
| 18 |
+
print(f"π§ Running on device: {self.device}")
|
| 19 |
+
|
| 20 |
+
# Load vision+language model with gated access
|
| 21 |
+
self.processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 22 |
+
self.model = MllamaForConditionalGeneration.from_pretrained(
|
| 23 |
+
model_id,
|
| 24 |
torch_dtype=torch.float32,
|
| 25 |
trust_remote_code=True
|
| 26 |
).to(self.device)
|
| 27 |
+
|
| 28 |
+
# Speech-to-text
|
| 29 |
self.stt = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
|
| 30 |
+
|
| 31 |
def analyze(self, image, question):
|
| 32 |
if image is None:
|
| 33 |
+
return "β Please upload a medical image."
|
| 34 |
+
prompt = question.strip() or "Analyze this medical image and share any abnormalities."
|
| 35 |
+
|
| 36 |
+
inputs = self.processor(
|
| 37 |
+
images=image, text=prompt, return_tensors="pt"
|
| 38 |
+
).to(self.device)
|
| 39 |
+
outputs = self.model.generate(**inputs, max_new_tokens=200, temperature=0.7)
|
| 40 |
+
return self.processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 41 |
+
|
| 42 |
def tts(self, text):
|
| 43 |
tts = gTTS(text=text, lang="en")
|
| 44 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 45 |
+
tts.save(tmp)
|
| 46 |
+
return tmp
|
| 47 |
+
|
| 48 |
def respond(self, image, audio, text):
|
| 49 |
+
question = text.strip()
|
| 50 |
if audio:
|
| 51 |
+
res = self.stt(audio)
|
| 52 |
+
q = res.get("text", "").strip() if isinstance(res, dict) else str(res).strip()
|
| 53 |
+
if q:
|
| 54 |
+
question = q
|
| 55 |
+
resp = self.analyze(image, question)
|
| 56 |
voice = self.tts(resp)
|
| 57 |
+
return resp, voice, question
|
| 58 |
|
| 59 |
+
# Initialize model
|
| 60 |
doctor = AIDoctor()
|
| 61 |
|
| 62 |
+
# ββββββββββββββββ
|
| 63 |
+
with gr.Blocks(title="π₯ AI Doctor with Llamaβ―3.2 Vision") as demo:
|
| 64 |
+
gr.Markdown("## AI Doctor β Vision + Voice powered by Llamaβ―3.2βVisionβInstruct")
|
| 65 |
+
|
| 66 |
with gr.Row():
|
| 67 |
+
img_in = gr.Image(label="Upload Medical Image", type="pil")
|
| 68 |
+
aud_in = gr.Audio(label="Ask by Voice", type="filepath")
|
| 69 |
+
txt_in = gr.Textbox(label="Ask by Text", lines=2)
|
| 70 |
+
|
| 71 |
resp_out = gr.Textbox(label="AI Response", lines=10)
|
| 72 |
aud_out = gr.Audio(label="AI Speaks", type="filepath")
|
| 73 |
+
q_out = gr.Textbox(label="Processed Question", lines=1)
|
| 74 |
+
|
| 75 |
+
btn = gr.Button("Ask AI Doctor")
|
| 76 |
+
btn.click(fn=doctor.respond,
|
| 77 |
+
inputs=[img_in, aud_in, txt_in],
|
| 78 |
outputs=[resp_out, aud_out, q_out])
|
| 79 |
demo.launch()
|