vilarin commited on
Commit
8e4e648
1 Parent(s): ac56402

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -8
app.py CHANGED
@@ -6,6 +6,9 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStream
6
  import os
7
  from threading import Thread
8
 
 
 
 
9
 
10
 
11
  MODEL_LIST = ["THUDM/glm-4v-9b"]
@@ -15,7 +18,14 @@ MODEL_NAME = MODEL_ID.split("/")[-1]
15
 
16
  TITLE = "<h1>VL-Chatbox</h1>"
17
 
18
- DESCRIPTION = f'<center><p>A SPACE FOR VLM MODELS</p><br><h3>MODEL NOW: <a href="https://hf.co/{MODEL_ID}">{MODEL_NAME}</a></center></h3>'
 
 
 
 
 
 
 
19
 
20
  CSS = """
21
  .duplicate-button {
@@ -40,28 +50,86 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
40
  model.eval()
41
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  @spaces.GPU()
44
  def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
45
  print(f'message is - {message}')
46
  print(f'history is - {history}')
47
  conversation = []
 
48
  if message["files"]:
49
- image = Image.open(message["files"][-1]).convert('RGB')
50
- conversation.append({"role": "user", "image": image, "content": message['text']})
 
 
 
 
51
  else:
52
  if len(history) == 0:
53
  #raise gr.Error("Please upload an image first.")
54
- image = None
55
  conversation.append({"role": "user", "content": message['text']})
56
  else:
57
  #image = Image.open(history[0][0][0])
58
  for prompt, answer in history:
59
  if answer is None:
60
- image = Image.open(prompt[0])
61
  conversation.extend([{"role": "user", "content": ""},{"role": "assistant", "content": ""}])
62
  else:
63
  conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
64
- conversation.append({"role": "user", "image": image, "content": message['text']})
 
 
 
 
 
65
  print(f"Conversation is -\n{conversation}")
66
 
67
  input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
@@ -93,7 +161,6 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
93
  chatbot = gr.Chatbot(height=450)
94
  chat_input = gr.MultimodalTextbox(
95
  interactive=True,
96
- file_types=["image"],
97
  placeholder="Enter message or upload a file one time...",
98
  show_label=False,
99
 
@@ -104,7 +171,7 @@ EXAMPLES = [
104
  [{"text": "Is it real?", "files": ["./spacecat.png"]}]
105
  ]
106
 
107
- with gr.Blocks(css=CSS) as demo:
108
  gr.HTML(TITLE)
109
  gr.HTML(DESCRIPTION)
110
  gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
 
6
  import os
7
  from threading import Thread
8
 
9
+ from langchain_community.document_loaders import PyMuPDFLoader
10
+ import docx
11
+ from pptx import Presentation
12
 
13
 
14
  MODEL_LIST = ["THUDM/glm-4v-9b"]
 
18
 
19
  TITLE = "<h1>VL-Chatbox</h1>"
20
 
21
+ DESCRIPTION = f"""
22
+ <center>
23
+ <p>A SPACE FOR MY FAV VLM.
24
+ <br>
25
+ MODEL NOW: <a href="https://hf.co/{MODEL_ID}">{MODEL_NAME}</a>
26
+ <br>
27
+ TIPS: NOW SUPPORT DM & ONE IMAGE/FILE UPLOAD PER TIME.
28
+ </p></center>"""
29
 
30
  CSS = """
31
  .duplicate-button {
 
50
  model.eval()
51
 
52
 
53
+ def extract_text(path):
54
+ return open(path, 'r').read()
55
+
56
+ def extract_pdf(path):
57
+ loader = PyMuPDFLoader(path)
58
+ data = loader.load()
59
+ data = [x.page_content for x in data]
60
+ content = '\n\n'.join(data)
61
+ return content
62
+
63
+ def extract_docx(path):
64
+ doc = docx.Document(path)
65
+ data = []
66
+ for paragraph in doc.paragraphs:
67
+ data.append(paragraph.text)
68
+ content = '\n\n'.join(data)
69
+
70
+ def extract_pptx(path):
71
+ prs = Presentation(path)
72
+ text = ""
73
+ for slide in prs.slides:
74
+ for shape in slide.shapes:
75
+ if hasattr(shape, "text"):
76
+ text += shape.text + "\n"
77
+ return text
78
+
79
+ def mode_load(path):
80
+ choice = ""
81
+ file_type = path.split(".")[-1]
82
+ if file_type in ["pdf", "txt", "py", "docx", "pptx", "json", "cpp", "md"]:
83
+ if file_type.endswith(".pdf"):
84
+ content = extract_pdf(path)
85
+ elif file_type.endswith(".docx"):
86
+ content = extract_docx(path)
87
+ elif file_type.endswith(".pptx"):
88
+ content = extract_pptx(path)
89
+ else:
90
+ content = extract_text(path)
91
+ choice = "doc"
92
+ print(content)
93
+ return choice, content
94
+ elif file_type in ["png", "jpg", "jpeg", "bmp", "tiff", "webp"]:
95
+ content = Image.open(path).convert('RGB')
96
+ choice = "image"
97
+ return choice, content
98
+ else:
99
+ raise gr.Error("Oops, unsupported files.")
100
+
101
  @spaces.GPU()
102
  def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
103
  print(f'message is - {message}')
104
  print(f'history is - {history}')
105
  conversation = []
106
+ prompt_files = []
107
  if message["files"]:
108
+ choice, contents = mode_load(message["files"][-1])
109
+ if choice == "image":
110
+ conversation.append({"role": "user", "image": contents, "content": message['text']})
111
+ elif choice == "doc":
112
+ format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
113
+ conversation.append({"role": "user", "content": format_msg})
114
  else:
115
  if len(history) == 0:
116
  #raise gr.Error("Please upload an image first.")
117
+ contents = None
118
  conversation.append({"role": "user", "content": message['text']})
119
  else:
120
  #image = Image.open(history[0][0][0])
121
  for prompt, answer in history:
122
  if answer is None:
123
+ prompt_files.append(prompt[0])
124
  conversation.extend([{"role": "user", "content": ""},{"role": "assistant", "content": ""}])
125
  else:
126
  conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
127
+ choice, contents = mode_load(prompt_files[-1])
128
+ if choice == "image":
129
+ conversation.append({"role": "user", "image": contents, "content": message['text']})
130
+ elif choice == "doc":
131
+ format_msg = contents + "\n\n\n" + "{} files uploaded.\n" + message['text']
132
+ conversation.append({"role": "user", "content": format_msg})
133
  print(f"Conversation is -\n{conversation}")
134
 
135
  input_ids = tokenizer.apply_chat_template(conversation, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
 
161
  chatbot = gr.Chatbot(height=450)
162
  chat_input = gr.MultimodalTextbox(
163
  interactive=True,
 
164
  placeholder="Enter message or upload a file one time...",
165
  show_label=False,
166
 
 
171
  [{"text": "Is it real?", "files": ["./spacecat.png"]}]
172
  ]
173
 
174
+ with gr.Blocks(css=CSS, theme="soft") as demo:
175
  gr.HTML(TITLE)
176
  gr.HTML(DESCRIPTION)
177
  gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")