Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,58 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from transformers import AutoProcessor,
|
| 5 |
-
from
|
| 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="
|
| 17 |
self.device = "cpu"
|
| 18 |
-
print(
|
| 19 |
-
|
| 20 |
-
|
| 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 "
|
| 34 |
-
prompt = question.strip() or "Analyze this medical image
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
|
| 45 |
-
tts.save(
|
| 46 |
-
return
|
| 47 |
|
| 48 |
def respond(self, image, audio, text):
|
| 49 |
-
|
| 50 |
if audio:
|
| 51 |
res = self.stt(audio)
|
| 52 |
-
|
| 53 |
-
if q
|
| 54 |
-
|
| 55 |
-
resp = self.analyze(image, question)
|
| 56 |
voice = self.tts(resp)
|
| 57 |
-
return resp, voice,
|
| 58 |
|
| 59 |
-
# Initialize model
|
| 60 |
doctor = AIDoctor()
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
gr.Markdown("## AI Doctor β Vision + Voice powered by Llamaβ―3.2βVisionβInstruct")
|
| 65 |
-
|
| 66 |
with gr.Row():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 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()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
|
| 5 |
+
from transformers import pipeline
|
| 6 |
from gtts import gTTS
|
| 7 |
import tempfile
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class AIDoctor:
|
| 10 |
+
def __init__(self, model_id="Qwen/Qwen2.5-VL-7B-Instruct-quantized.w8a8"):
|
| 11 |
self.device = "cpu"
|
| 12 |
+
print("π Using device:", self.device)
|
| 13 |
+
self.proc = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 14 |
+
self.model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
|
|
|
|
|
| 15 |
model_id,
|
| 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.strip() or "Analyze this medical image for abnormalities."
|
| 25 |
+
inputs = self.proc(images=image, text=prompt, return_tensors="pt").to(self.device)
|
| 26 |
+
out = self.model.generate(**inputs, max_new_tokens=150, temperature=0.7)
|
| 27 |
+
return self.proc.decode(out[0], skip_special_tokens=True).strip()
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def tts(self, text):
|
| 30 |
tts = gTTS(text=text, lang="en")
|
| 31 |
+
path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 32 |
+
tts.save(path)
|
| 33 |
+
return path
|
| 34 |
|
| 35 |
def respond(self, image, audio, text):
|
| 36 |
+
q = text.strip()
|
| 37 |
if audio:
|
| 38 |
res = self.stt(audio)
|
| 39 |
+
q_trans = res.get("text", "").strip() if isinstance(res, dict) else str(res)
|
| 40 |
+
if q_trans: q = q_trans
|
| 41 |
+
resp = self.analyze(image, q)
|
|
|
|
| 42 |
voice = self.tts(resp)
|
| 43 |
+
return resp, voice, q
|
| 44 |
|
|
|
|
| 45 |
doctor = AIDoctor()
|
| 46 |
|
| 47 |
+
with gr.Blocks(title="AI Doctor (Qwenβ―2.5βVLβ7B INT8)") as demo:
|
| 48 |
+
gr.Markdown("### AI Doctor with Qwenβ―2.5βVLβ7B (quantized INT8)")
|
|
|
|
|
|
|
| 49 |
with gr.Row():
|
| 50 |
+
img = gr.Image(label="Upload medical image", type="pil")
|
| 51 |
+
aud = gr.Audio(label="Ask by voice", type="filepath")
|
| 52 |
+
txt = gr.Textbox(label="Ask by text", lines=2)
|
| 53 |
+
out_txt = gr.Textbox(label="AI Response", lines=10)
|
| 54 |
+
out_aud = gr.Audio(label="AI Speaks", type="filepath")
|
| 55 |
+
q_out = gr.Textbox(label="Processed question", lines=1)
|
| 56 |
+
btn = gr.Button("Ask Doctor")
|
| 57 |
+
btn.click(fn=doctor.respond, inputs=[img, aud, txt], outputs=[out_txt, out_aud, q_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
demo.launch()
|