Spaces:
Paused
Paused
kgupta21 commited on
Commit ·
746ae2b
1
Parent(s): eb5c340
local inference page with fixes to gpu with zerogpu + add accelerate for device mapping - removed previous and fixed overall
Browse filesFiles changed (2)
- app.py+69-98
- requirements.txt+6-5
20 unmodified lines212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787953 unmodified lines13313413513613713813914010 unmodified lines15115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021167 unmodified lines27928028128228328428528628728828929029129212 unmodified lines3053063073083093103113123133143153169 unmodified lines32632732832933033133237 unmodified lines37037137237320 unmodified linesAPP_VERSION = "1.0.0"logger.info(f"Starting Radiology Teaching App v{APP_VERSION}")# Global variablespipe = Nonellm = Nonetokenizer = Nonedevice = 0 if torch.cuda.is_available() else "cpu"logger.info(f"Using device: {device}")# Initialize WhisperMODEL_NAME = "openai/whisper-large-v3-turbo"BATCH_SIZE = 8FILE_LIMIT_MB = 5000try:logger.info("Initializing Whisper model...")pipe = pipeline(task="automatic-speech-recognition",model=MODEL_NAME,chunk_length_s=30,device=device,)except Exception as e:logger.error(f"Error initializing Whisper model: {str(e)}")pipe = None# Initialize Llamatry:logger.info("Initializing Llama model...")llm_model_id = "chuanli11/Llama-3.2-3B-Instruct-uncensored"# Initialize tokenizer firsttokenizer = AutoTokenizer.from_pretrained(llm_model_id)tokenizer.use_default_system_prompt = False# Initialize model with proper device mappingif torch.cuda.is_available():logger.info("Loading Llama model on GPU...")llm = AutoModelForCausalLM.from_pretrained(llm_model_id,torch_dtype=torch.float16,device_map="auto",load_in_8bit=True # Use 8-bit quantization to reduce memory usage)else:logger.info("Loading Llama model on CPU...")llm = AutoModelForCausalLM.from_pretrained(llm_model_id,device_map={"": "cpu"},low_cpu_mem_usage=True)except Exception as e:logger.error(f"Error initializing Llama model: {str(e)}")llm = Nonetokenizer = Nonetry:# Load only 10 rows from the dataset53 unmodified lines"""Transcribe audio using Whisper"""if inputs is None:raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")if pipe is None:raise gr.Error("Whisper model not initialized properly!")try:logger.info("Transcribing audio...")10 unmodified linesground_truth_impression: str,max_new_tokens: int = 1024,temperature: float = 0.6,) -> Iterator[str]:"""Analyze transcribed report against ground truth using Llama"""global llm, tokenizer # Add global declarationif llm is None or tokenizer is None:raise gr.Error("Llama model not initialized properly!")try:task_prompt = f"""You are an expert radiologist. Compare the following transcribed radiology report with the ground truth and provide detailed feedback.Transcribed Report:{transcribed_text}Ground Truth Findings:{ground_truth_findings}Ground Truth Impression:{ground_truth_impression}Please analyze:1. Accuracy of findings2. Completeness of report3. Structure and clarity4. Areas for improvementProvide your analysis in a clear, structured format."""conversation = [{"role": "system", "content": "You are an expert radiologist providing detailed feedback."},{"role": "user", "content": task_prompt}]input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")input_ids = input_ids.to(llm.device)streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)generate_kwargs = dict(input_ids=input_ids,streamer=streamer,max_new_tokens=max_new_tokens,do_sample=True,temperature=temperature,num_beams=1,)t = Thread(target=llm.generate, kwargs=generate_kwargs)t.start()outputs = []for text in streamer:outputs.append(text)yield "".join(outputs)except Exception as e:logger.error(f"Error in Llama analysis: {str(e)}")raise gr.Error(f"Analysis failed: {str(e)}")def load_random_case(hide_ground_truth):try:67 unmodified lines# Load case for comparisonload_case_btn = gr.Button("Load Random Case for Comparison")local_ground_truth_findings = gr.Textbox(label="Ground Truth Findings", lines=5, interactive=False)local_ground_truth_impression = gr.Textbox(label="Ground Truth Impression", lines=5, interactive=False)with gr.Column():# Editable transcription and analysis interfaceedited_transcription = gr.Textbox(label="Edit Transcription", lines=10)temperature_input = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.6, step=0.1)max_tokens_input = gr.Slider(label="Max Tokens", minimum=256, maximum=2048, value=1024, step=128)analyze_btn = gr.Button("Analyze with Llama")llama_analysis_output = gr.Textbox(label="Llama Analysis Output", lines=15, interactive=False)12 unmodified lines)# Load case for local analysislocal_image_display = gr.Image(label="Chest X-ray Image", type="pil") # Add this lineload_case_btn.click(fn=load_random_case,inputs=[gr.Checkbox(value=False, visible=False)], # Hidden checkbox for hide_ground_truthoutputs=[local_image_display, # Update this linelocal_ground_truth_findings,local_ground_truth_impression,gr.State(), # Hidden state9 unmodified lineslocal_ground_truth_findings,local_ground_truth_impression,max_tokens_input,temperature_input],outputs=llama_analysis_output)37 unmodified lines)logger.info("Starting Gradio interface...")demo.launch()20 unmodified lines21222324252627282930313233343536373839404142434445464753 unmodified lines10110210310410510610 unmodified lines11711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517667 unmodified lines24424524624724824925025125225325425525625725825926026112 unmodified lines2742752762772782792802812822832849 unmodified lines29429529629729829930030130230337 unmodified lines34134234334420 unmodified linesAPP_VERSION = "1.0.0"logger.info(f"Starting Radiology Teaching App v{APP_VERSION}")# Model configurationMODEL_NAME = "openai/whisper-large-v3-turbo"BATCH_SIZE = 8FILE_LIMIT_MB = 5000MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))device = 0 if torch.cuda.is_available() else "cpu"# Initialize the LLMif torch.cuda.is_available():llm_model_id = "chuanli11/Llama-3.2-3B-Instruct-uncensored"llm = AutoModelForCausalLM.from_pretrained(llm_model_id, torch_dtype=torch.float16, device_map="auto")tokenizer = AutoTokenizer.from_pretrained(llm_model_id)tokenizer.use_default_system_prompt = False# Initialize the transcription pipelinepipe = pipeline(task="automatic-speech-recognition",model=MODEL_NAME,chunk_length_s=30,device=device,)try:# Load only 10 rows from the dataset53 unmodified lines"""Transcribe audio using Whisper"""if inputs is None:raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")try:logger.info("Transcribing audio...")10 unmodified linesground_truth_impression: str,max_new_tokens: int = 1024,temperature: float = 0.6,top_p: float = 0.9,top_k: int = 50,repetition_penalty: float = 1.2,) -> Iterator[str]:"""Analyze transcribed report against ground truth using Llama"""task_prompt = f"""You are an expert radiologist. Compare the following transcribed radiology report with the ground truth and provide detailed feedback.Transcribed Report:{transcribed_text}Ground Truth Findings:{ground_truth_findings}Ground Truth Impression:{ground_truth_impression}Please analyze:1. Accuracy of findings2. Completeness of report3. Structure and clarity4. Areas for improvementProvide your analysis in a clear, structured format."""conversation = [{"role": "system", "content": "You are an expert radiologist providing detailed feedback."},{"role": "user", "content": task_prompt}]input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")input_ids = input_ids.to(llm.device)streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)generate_kwargs = dict({"input_ids": input_ids},streamer=streamer,max_new_tokens=max_new_tokens,do_sample=True,top_p=top_p,top_k=top_k,temperature=temperature,num_beams=1,repetition_penalty=repetition_penalty,)t = Thread(target=llm.generate, kwargs=generate_kwargs)t.start()outputs = []for text in streamer:outputs.append(text)yield "".join(outputs)def load_random_case(hide_ground_truth):try:67 unmodified lines# Load case for comparisonload_case_btn = gr.Button("Load Random Case for Comparison")local_image_display = gr.Image(label="Chest X-ray Image", type="pil")local_ground_truth_findings = gr.Textbox(label="Ground Truth Findings", lines=5, interactive=False)local_ground_truth_impression = gr.Textbox(label="Ground Truth Impression", lines=5, interactive=False)with gr.Column():# Editable transcription and analysis interfaceedited_transcription = gr.Textbox(label="Edit Transcription", lines=10)temperature_input = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, value=0.6, step=0.1)top_p_input = gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, value=0.9, step=0.05)top_k_input = gr.Slider(label="Top-k", minimum=1, maximum=1000, value=50, step=1)max_tokens_input = gr.Slider(label="Max Tokens", minimum=256, maximum=2048, value=1024, step=128)repetition_penalty_input = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, value=1.2, step=0.05)analyze_btn = gr.Button("Analyze with Llama")llama_analysis_output = gr.Textbox(label="Llama Analysis Output", lines=15, interactive=False)12 unmodified lines)# Load case for local analysisload_case_btn.click(fn=load_random_case,inputs=[gr.Checkbox(value=False, visible=False)], # Hidden checkbox for hide_ground_truthoutputs=[local_image_display,local_ground_truth_findings,local_ground_truth_impression,gr.State(), # Hidden state9 unmodified lineslocal_ground_truth_findings,local_ground_truth_impression,max_tokens_input,temperature_input,top_p_input,top_k_input,repetition_penalty_input],outputs=llama_analysis_output)37 unmodified lines)logger.info("Starting Gradio interface...")demo.queue().launch(ssr_mode=False)
12345678910gradio>=4.16.0pandas>=2.0.0datasets>=2.15.0openai>=1.0.0Pillow>=10.0.0huggingface-hub>=0.20.0torch>=2.0.0transformers>=4.36.0spaces>=0.19.3accelerate>=0.27.01234567891011transformersgradiotorchaccelerateSentencePiecepandas>=2.0.0datasets>=2.15.0openai>=1.0.0Pillow>=10.0.0huggingface-hub>=0.20.0spaces>=0.19.3
app.py+69-98
requirements.txt+6-5