Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import torch
|
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import spaces
|
5 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
import os
|
7 |
from threading import Thread
|
8 |
|
@@ -34,15 +34,19 @@ h1 {
|
|
34 |
}
|
35 |
"""
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
def extract_text(path):
|
43 |
return open(path, 'r').read()
|
44 |
|
45 |
-
|
46 |
def extract_pdf(path):
|
47 |
doc = pymupdf.open(path)
|
48 |
text = ""
|
@@ -50,7 +54,6 @@ def extract_pdf(path):
|
|
50 |
text += page.get_text()
|
51 |
return text
|
52 |
|
53 |
-
|
54 |
def extract_docx(path):
|
55 |
doc = docx.Document(path)
|
56 |
data = []
|
@@ -59,7 +62,6 @@ def extract_docx(path):
|
|
59 |
content = '\n\n'.join(data)
|
60 |
return content
|
61 |
|
62 |
-
|
63 |
def extract_pptx(path):
|
64 |
prs = Presentation(path)
|
65 |
text = ""
|
@@ -69,7 +71,6 @@ def extract_pptx(path):
|
|
69 |
text += shape.text + "\n"
|
70 |
return text
|
71 |
|
72 |
-
|
73 |
def mode_load(path):
|
74 |
choice = ""
|
75 |
file_type = path.split(".")[-1]
|
@@ -87,7 +88,6 @@ def mode_load(path):
|
|
87 |
print(content[:100])
|
88 |
return choice, content[:5000]
|
89 |
|
90 |
-
|
91 |
elif file_type in ["png", "jpg", "jpeg", "bmp", "tiff", "webp"]:
|
92 |
content = Image.open(path).convert('RGB')
|
93 |
choice = "image"
|
@@ -96,7 +96,6 @@ def mode_load(path):
|
|
96 |
else:
|
97 |
raise gr.Error("Oops, unsupported files.")
|
98 |
|
99 |
-
|
100 |
@spaces.GPU()
|
101 |
def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
|
102 |
|
@@ -104,7 +103,9 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
104 |
MODEL_ID,
|
105 |
torch_dtype=torch.bfloat16,
|
106 |
low_cpu_mem_usage=True,
|
107 |
-
trust_remote_code=True
|
|
|
|
|
108 |
)
|
109 |
|
110 |
print(f'message is - {message}')
|
@@ -120,11 +121,9 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
120 |
conversation.append({"role": "user", "content": format_msg})
|
121 |
else:
|
122 |
if len(history) == 0:
|
123 |
-
# raise gr.Error("Please upload an image first.")
|
124 |
contents = None
|
125 |
conversation.append({"role": "user", "content": message['text']})
|
126 |
else:
|
127 |
-
# image = Image.open(history[0][0][0])
|
128 |
for prompt, answer in history:
|
129 |
if answer is None:
|
130 |
prompt_files.append(prompt[0])
|
@@ -137,7 +136,6 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
137 |
choice = ""
|
138 |
conversation.append({"role": "user", "image": "", "content": message['text']})
|
139 |
|
140 |
-
|
141 |
if choice == "image":
|
142 |
conversation.append({"role": "user", "image": contents, "content": message['text']})
|
143 |
elif choice == "doc":
|
@@ -169,19 +167,13 @@ def stream_chat(message, history: list, temperature: float, max_length: int, top
|
|
169 |
buffer += new_text
|
170 |
yield buffer
|
171 |
|
172 |
-
|
173 |
-
chatbot = gr.Chatbot(
|
174 |
-
#rtl=True,
|
175 |
-
)
|
176 |
chat_input = gr.MultimodalTextbox(
|
177 |
interactive=True,
|
178 |
placeholder="Enter message or upload a file ...",
|
179 |
show_label=False,
|
180 |
-
#rtl=True,
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
)
|
|
|
185 |
EXAMPLES = [
|
186 |
[{"text": "Write a poem about spring season in French Language", }],
|
187 |
[{"text": "what does this chart mean?", "files": ["sales.png"]}],
|
@@ -195,8 +187,6 @@ with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
|
|
195 |
gr.ChatInterface(
|
196 |
fn=stream_chat,
|
197 |
multimodal=True,
|
198 |
-
|
199 |
-
|
200 |
textbox=chat_input,
|
201 |
chatbot=chatbot,
|
202 |
fill_height=True,
|
@@ -247,5 +237,4 @@ with gr.Blocks(css=CSS, theme="soft", fill_height=True) as demo:
|
|
247 |
gr.Examples(EXAMPLES, [chat_input])
|
248 |
|
249 |
if __name__ == "__main__":
|
250 |
-
|
251 |
-
demo.queue(api_open=False).launch(show_api=False, share=False, )#server_name="0.0.0.0", )
|
|
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import spaces
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
6 |
import os
|
7 |
from threading import Thread
|
8 |
|
|
|
34 |
}
|
35 |
"""
|
36 |
|
37 |
+
# Configure BitsAndBytes for 4-bit quantization
|
38 |
+
quantization_config = BitsAndBytesConfig(
|
39 |
+
load_in_4bit=True,
|
40 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
41 |
+
bnb_4bit_quant_type="nf4",
|
42 |
+
bnb_4bit_use_double_quant=True,
|
43 |
+
)
|
44 |
|
45 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
46 |
|
|
|
|
|
47 |
def extract_text(path):
|
48 |
return open(path, 'r').read()
|
49 |
|
|
|
50 |
def extract_pdf(path):
|
51 |
doc = pymupdf.open(path)
|
52 |
text = ""
|
|
|
54 |
text += page.get_text()
|
55 |
return text
|
56 |
|
|
|
57 |
def extract_docx(path):
|
58 |
doc = docx.Document(path)
|
59 |
data = []
|
|
|
62 |
content = '\n\n'.join(data)
|
63 |
return content
|
64 |
|
|
|
65 |
def extract_pptx(path):
|
66 |
prs = Presentation(path)
|
67 |
text = ""
|
|
|
71 |
text += shape.text + "\n"
|
72 |
return text
|
73 |
|
|
|
74 |
def mode_load(path):
|
75 |
choice = ""
|
76 |
file_type = path.split(".")[-1]
|
|
|
88 |
print(content[:100])
|
89 |
return choice, content[:5000]
|
90 |
|
|
|
91 |
elif file_type in ["png", "jpg", "jpeg", "bmp", "tiff", "webp"]:
|
92 |
content = Image.open(path).convert('RGB')
|
93 |
choice = "image"
|
|
|
96 |
else:
|
97 |
raise gr.Error("Oops, unsupported files.")
|
98 |
|
|
|
99 |
@spaces.GPU()
|
100 |
def stream_chat(message, history: list, temperature: float, max_length: int, top_p: float, top_k: int, penalty: float):
|
101 |
|
|
|
103 |
MODEL_ID,
|
104 |
torch_dtype=torch.bfloat16,
|
105 |
low_cpu_mem_usage=True,
|
106 |
+
trust_remote_code=True,
|
107 |
+
quantization_config=quantization_config,
|
108 |
+
device_map="auto"
|
109 |
)
|
110 |
|
111 |
print(f'message is - {message}')
|
|
|
121 |
conversation.append({"role": "user", "content": format_msg})
|
122 |
else:
|
123 |
if len(history) == 0:
|
|
|
124 |
contents = None
|
125 |
conversation.append({"role": "user", "content": message['text']})
|
126 |
else:
|
|
|
127 |
for prompt, answer in history:
|
128 |
if answer is None:
|
129 |
prompt_files.append(prompt[0])
|
|
|
136 |
choice = ""
|
137 |
conversation.append({"role": "user", "image": "", "content": message['text']})
|
138 |
|
|
|
139 |
if choice == "image":
|
140 |
conversation.append({"role": "user", "image": contents, "content": message['text']})
|
141 |
elif choice == "doc":
|
|
|
167 |
buffer += new_text
|
168 |
yield buffer
|
169 |
|
170 |
+
chatbot = gr.Chatbot()
|
|
|
|
|
|
|
171 |
chat_input = gr.MultimodalTextbox(
|
172 |
interactive=True,
|
173 |
placeholder="Enter message or upload a file ...",
|
174 |
show_label=False,
|
|
|
|
|
|
|
|
|
175 |
)
|
176 |
+
|
177 |
EXAMPLES = [
|
178 |
[{"text": "Write a poem about spring season in French Language", }],
|
179 |
[{"text": "what does this chart mean?", "files": ["sales.png"]}],
|
|
|
187 |
gr.ChatInterface(
|
188 |
fn=stream_chat,
|
189 |
multimodal=True,
|
|
|
|
|
190 |
textbox=chat_input,
|
191 |
chatbot=chatbot,
|
192 |
fill_height=True,
|
|
|
237 |
gr.Examples(EXAMPLES, [chat_input])
|
238 |
|
239 |
if __name__ == "__main__":
|
240 |
+
demo.queue(api_open=False).launch(show_api=False, share=False)
|
|