sounar commited on
Commit
b43d847
1 Parent(s): a230a1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -60
app.py CHANGED
@@ -1,66 +1,32 @@
1
- from flask import Flask, request, jsonify
2
- import torch
3
- from PIL import Image
4
- from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig
5
 
6
  # Get API token from environment variable
7
- api_token = os.getenv("HF_TOKEN").strip()
8
 
9
- # Model configuration
10
- bnb_config = BitsAndBytesConfig(
11
- load_in_4bit=True,
12
- bnb_4bit_quant_type="nf4",
13
- bnb_4bit_use_double_quant=True,
14
- bnb_4bit_compute_dtype=torch.float16,
15
- )
16
 
17
- # Model and tokenizer loading
18
- model = AutoModel.from_pretrained(
19
- "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
20
- quantization_config=bnb_config,
21
- device_map="auto",
22
- torch_dtype=torch.float16,
23
- trust_remote_code=True,
24
- attn_implementation="flash_attention_2",
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
- tokenizer = AutoTokenizer.from_pretrained(
27
- "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
28
- trust_remote_code=True
29
- )
30
-
31
-
32
-
33
- app = Flask(__name__)
34
-
35
- # Model configuration and loading (same as before)
36
-
37
- @app.route('/analyze', methods=['POST'])
38
- def analyze():
39
- image = request.files['image']
40
- question = request.form['question']
41
-
42
- # Preprocess image
43
- image = Image.open(image).convert('RGB')
44
-
45
- # Prepare input
46
- msgs = [{'role': 'user', 'content': [image, question]}]
47
-
48
- # Generate response
49
- res = model.chat(
50
- image=image,
51
- msgs=msgs,
52
- tokenizer=tokenizer,
53
- sampling=True,
54
- temperature=0.95,
55
- stream=True
56
- )
57
-
58
- # Process response
59
- generated_text = ""
60
- for new_text in res:
61
- generated_text += new_text
62
-
63
- return jsonify({'response': generated_text})
64
 
65
- if __name__ == '__main__':
66
- app.run(debug=True)
 
1
+
 
 
 
2
 
3
  # Get API token from environment variable
4
+ #api_token = os.getenv("HF_TOKEN").strip()
5
 
6
+ import gradio as gr
7
+ from transformers import AutoModel, AutoTokenizer
8
+ import torch
 
 
 
 
9
 
10
+ # Load the model and tokenizer
11
+ model_name = "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1"
12
+ model = AutoModel.from_pretrained(model_name, trust_remote_code=True, device_map="auto", torch_dtype=torch.float16)
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
14
+
15
+ def process_query(image, question):
16
+ inputs = {"question": question}
17
+ if image:
18
+ inputs["image"] = image
19
+
20
+ # Process the inputs and generate a response
21
+ response = model.chat(image=inputs.get("image"), msgs=[{"role": "user", "content": question}], tokenizer=tokenizer)
22
+ return response
23
+
24
+ iface = gr.Interface(
25
+ fn=process_query,
26
+ inputs=[gr.Image(label="Upload Medical Image"), gr.Textbox(label="Question")],
27
+ outputs="text",
28
+ title="Medical Multimodal Assistant",
29
+ description="Upload a medical image and ask your question."
30
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ iface.launch()