Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import io
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
|
| 7 |
+
|
| 8 |
+
title = """# Welcome to🌟Tonic's CheXRay⚕⚛ !
|
| 9 |
+
You can use this ZeroGPU Space to test out the current model [StanfordAIMI/CheXagent-8b](https://huggingface.co/StanfordAIMI/CheXagent-8b). CheXRay⚕⚛ is fine tuned to analyze chest x-rays with a different and generally better results than other multimodal models.
|
| 10 |
+
You can also useCheXRay⚕⚛ by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/CheXRay?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
|
| 11 |
+
|
| 12 |
+
### How To use
|
| 13 |
+
|
| 14 |
+
simply upload an image with the right prompt (coming soon!) and anaylze your Xray !
|
| 15 |
+
|
| 16 |
+
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [Poly](https://github.com/tonic-ai/poly) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
device = "cuda"
|
| 20 |
+
dtype = torch.float16
|
| 21 |
+
|
| 22 |
+
processor = AutoProcessor.from_pretrained("StanfordAIMI/CheXagent-8b", trust_remote_code=True)
|
| 23 |
+
generation_config = GenerationConfig.from_pretrained("StanfordAIMI/CheXagent-8b")
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained("StanfordAIMI/CheXagent-8b", torch_dtype=dtype, trust_remote_code=True)
|
| 25 |
+
|
| 26 |
+
@spaces.GPU
|
| 27 |
+
def generate(image, prompt):
|
| 28 |
+
# Convert the uploaded file to an image and process
|
| 29 |
+
image = Image.open(io.BytesIO(image.read())).convert("RGB")
|
| 30 |
+
images = [image]
|
| 31 |
+
|
| 32 |
+
# Prepare inputs
|
| 33 |
+
inputs = processor(images=images, text=f" USER: <s>{prompt} ASSISTANT: <s>", return_tensors="pt").to(device=device, dtype=dtype)
|
| 34 |
+
|
| 35 |
+
# Generate the findings
|
| 36 |
+
output = model.generate(**inputs, generation_config=generation_config)[0]
|
| 37 |
+
response = processor.tokenizer.decode(output, skip_special_tokens=True)
|
| 38 |
+
return response
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# AI Medical Image Analysis")
|
| 43 |
+
gr.Markdown("Upload a medical image and enter a prompt to receive an AI-generated analysis.")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
image_input = gr.Image(type="file")
|
| 48 |
+
prompt_input = gr.Textbox(label="Prompt")
|
| 49 |
+
with gr.Column():
|
| 50 |
+
output_text = gr.Textbox(label="Response")
|
| 51 |
+
|
| 52 |
+
generate_button = gr.Button("Generate")
|
| 53 |
+
generate_button.click(fn=generate, inputs=[image_input, prompt_input], outputs=output_text)
|
| 54 |
+
|
| 55 |
+
demo.launch()
|