MadsGalsgaard commited on
Commit
a4baa70
·
verified ·
1 Parent(s): f4f69e1

Example_updated_with_Lib

Browse files
Files changed (1) hide show
  1. app.py +2 -165
app.py CHANGED
@@ -112,158 +112,6 @@
112
  # if __name__ == "__main__":
113
  # demo.launch()
114
 
115
- ### 26 aug Use a pipeline as a high-level Logic
116
- # import spaces
117
- # import os
118
- # import subprocess
119
- # from llama_cpp import Llama
120
- # from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
121
- # from llama_cpp_agent.providers import LlamaCppPythonProvider
122
- # from llama_cpp_agent.chat_history import BasicChatHistory
123
- # from llama_cpp_agent.chat_history.messages import Roles
124
- # import gradio as gr
125
- # from huggingface_hub import hf_hub_download
126
-
127
- # huggingface_token = os.getenv("HF_TOKEN")
128
-
129
- # # Download the Meta-Llama-3.1-8B-Instruct model
130
- # hf_hub_download(
131
- # repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
132
- # filename="Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
133
- # local_dir="./models",
134
- # token=huggingface_token
135
- # )
136
-
137
- # llm = None
138
- # llm_model = None
139
-
140
- # @spaces.GPU(duration=120)
141
- # def respond(
142
- # message,
143
- # history: list[tuple[str, str]],
144
- # model,
145
- # system_message,
146
- # max_tokens,
147
- # temperature,
148
- # top_p,
149
- # top_k,
150
- # repeat_penalty,
151
- # ):
152
- # chat_template = MessagesFormatterType.GEMMA_2
153
-
154
- # global llm
155
- # global llm_model
156
-
157
- # # Load model only if it's not already loaded or if a new model is selected
158
- # if llm is None or llm_model != model:
159
- # try:
160
- # llm = Llama(
161
- # model_path=f"models/{model}",
162
- # flash_attn=True,
163
- # n_gpu_layers=81, # Adjust based on available GPU resources
164
- # n_batch=1024,
165
- # n_ctx=8192,
166
- # )
167
- # llm_model = model
168
- # except Exception as e:
169
- # return f"Error loading model: {str(e)}"
170
-
171
- # provider = LlamaCppPythonProvider(llm)
172
-
173
- # agent = LlamaCppAgent(
174
- # provider,
175
- # system_prompt=f"{system_message}",
176
- # predefined_messages_formatter_type=chat_template,
177
- # debug_output=True
178
- # )
179
-
180
- # settings = provider.get_provider_default_settings()
181
- # settings.temperature = temperature
182
- # settings.top_k = top_k
183
- # settings.top_p = top_p
184
- # settings.max_tokens = max_tokens
185
- # settings.repeat_penalty = repeat_penalty
186
- # settings.stream = True
187
-
188
- # messages = BasicChatHistory()
189
-
190
- # # Add user and assistant messages to the history
191
- # for msn in history:
192
- # user = {'role': Roles.user, 'content': msn[0]}
193
- # assistant = {'role': Roles.assistant, 'content': msn[1]}
194
- # messages.add_message(user)
195
- # messages.add_message(assistant)
196
-
197
- # # Stream the response
198
- # try:
199
- # stream = agent.get_chat_response(
200
- # message,
201
- # llm_sampling_settings=settings,
202
- # chat_history=messages,
203
- # returns_streaming_generator=True,
204
- # print_output=False
205
- # )
206
-
207
- # outputs = ""
208
- # for output in stream:
209
- # outputs += output
210
- # yield outputs
211
- # except Exception as e:
212
- # yield f"Error during response generation: {str(e)}"
213
-
214
- # description = """<p align="center">Using the Meta-Llama-3.1-8B-Instruct Model</p>"""
215
-
216
- # demo = gr.ChatInterface(
217
- # respond,
218
- # additional_inputs=[
219
- # gr.Dropdown([
220
- # 'Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf'
221
- # ],
222
- # value="Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
223
- # label="Model"
224
- # ),
225
- # gr.Textbox(value="You are a helpful assistant.", label="System message"),
226
- # gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
227
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
228
- # gr.Slider(
229
- # minimum=0.1,
230
- # maximum=1.0,
231
- # value=0.95,
232
- # step=0.05,
233
- # label="Top-p",
234
- # ),
235
- # gr.Slider(
236
- # minimum=0,
237
- # maximum=100,
238
- # value=40,
239
- # step=1,
240
- # label="Top-k",
241
- # ),
242
- # gr.Slider(
243
- # minimum=0.0,
244
- # maximum=2.0,
245
- # value=1.1,
246
- # step=0.1,
247
- # label="Repetition penalty",
248
- # ),
249
- # ],
250
- # retry_btn="Retry",
251
- # undo_btn="Undo",
252
- # clear_btn="Clear",
253
- # submit_btn="Send",
254
- # title="Chat with Meta-Llama-3.1-8B-Instruct using llama.cpp",
255
- # description=description,
256
- # chatbot=gr.Chatbot(
257
- # scale=1,
258
- # likeable=False,
259
- # show_copy_button=True
260
- # )
261
- # )
262
-
263
- # if __name__ == "__main__":
264
- # demo.launch()
265
-
266
-
267
 
268
  ####03 3.1 8b
269
 
@@ -583,7 +431,7 @@
583
 
584
 
585
 
586
- ###04
587
  from transformers import MllamaForConditionalGeneration, AutoProcessor, TextIteratorStreamer
588
  from PIL import Image
589
  import requests
@@ -659,18 +507,7 @@ def bot_streaming(message, history, max_new_tokens=250):
659
  yield buffer
660
 
661
 
662
- demo = gr.ChatInterface(fn=bot_streaming, title="Multimodal Llama", examples=[
663
- [{"text": "Which era does this piece belong to? Give details about the era.", "files":["./examples/rococo.jpg"]},
664
- 200],
665
- [{"text": "Where do the droughts happen according to this diagram?", "files":["./examples/weather_events.png"]},
666
- 250],
667
- [{"text": "What happens when you take out white cat from this chain?", "files":["./examples/ai2d_test.jpg"]},
668
- 250],
669
- [{"text": "How long does it take from invoice date to due date? Be short and concise.", "files":["./examples/invoice.png"]},
670
- 250],
671
- [{"text": "Where to find this monument? Can you give me other recommendations around the area?", "files":["./examples/wat_arun.jpg"]},
672
- 250],
673
- ],
674
  textbox=gr.MultimodalTextbox(),
675
  additional_inputs = [gr.Slider(
676
  minimum=10,
 
112
  # if __name__ == "__main__":
113
  # demo.launch()
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  ####03 3.1 8b
117
 
 
431
 
432
 
433
 
434
+ ###OCT04 LLAMA3.2 Vision Model
435
  from transformers import MllamaForConditionalGeneration, AutoProcessor, TextIteratorStreamer
436
  from PIL import Image
437
  import requests
 
507
  yield buffer
508
 
509
 
510
+ demo = gr.ChatInterface(fn=bot_streaming, title="Multimodal Llama",
 
 
 
 
 
 
 
 
 
 
 
511
  textbox=gr.MultimodalTextbox(),
512
  additional_inputs = [gr.Slider(
513
  minimum=10,