sam2ai commited on
Commit
a9aaecd
·
verified ·
1 Parent(s): a67d869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -2,7 +2,7 @@ from threading import Thread
2
  from typing import Dict
3
 
4
  import gradio as gr
5
- import spaces
6
  import torch
7
  from PIL import Image
8
  from transformers import AutoModelForVision2Seq, AutoProcessor, AutoTokenizer, TextIteratorStreamer
@@ -28,7 +28,7 @@ processor = AutoProcessor.from_pretrained(model_id)
28
  model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
29
 
30
 
31
- @spaces.GPU
32
  def stream_chat(message: Dict[str, str], history: list):
33
  # Turn 1:
34
  # {'text': 'what is this', 'files': ['image-xxx.jpg']}
@@ -39,8 +39,9 @@ def stream_chat(message: Dict[str, str], history: list):
39
  # [[('image-xxx.jpg',), None], ['what is this', 'a image.']]
40
 
41
  image_path = None
42
- if len(message["files"]) != 0:
43
- image_path = message["files"][0]
 
44
 
45
  if len(history) != 0 and isinstance(history[0][0], tuple):
46
  image_path = history[0][0][0]
@@ -57,7 +58,7 @@ def stream_chat(message: Dict[str, str], history: list):
57
  for prompt, answer in history:
58
  conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
59
 
60
- conversation.append({"role": "user", "content": message["text"]})
61
 
62
  input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
63
  image_token_id = tokenizer.convert_tokens_to_ids("<image>")
@@ -100,3 +101,4 @@ with gr.Blocks(css=CSS) as demo:
100
 
101
  if __name__ == "__main__":
102
  demo.launch()
 
 
2
  from typing import Dict
3
 
4
  import gradio as gr
5
+ # import spaces
6
  import torch
7
  from PIL import Image
8
  from transformers import AutoModelForVision2Seq, AutoProcessor, AutoTokenizer, TextIteratorStreamer
 
28
  model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
29
 
30
 
31
+ # @spaces.GPU
32
  def stream_chat(message: Dict[str, str], history: list):
33
  # Turn 1:
34
  # {'text': 'what is this', 'files': ['image-xxx.jpg']}
 
39
  # [[('image-xxx.jpg',), None], ['what is this', 'a image.']]
40
 
41
  image_path = None
42
+ # print(message.files[0].path)
43
+ if len(message.files) != 0:
44
+ image_path = message.files[0].path
45
 
46
  if len(history) != 0 and isinstance(history[0][0], tuple):
47
  image_path = history[0][0][0]
 
58
  for prompt, answer in history:
59
  conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
60
 
61
+ conversation.append({"role": "user", "content": message.text})
62
 
63
  input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
64
  image_token_id = tokenizer.convert_tokens_to_ids("<image>")
 
101
 
102
  if __name__ == "__main__":
103
  demo.launch()
104
+