darknoon commited on
Commit
697a1f0
·
1 Parent(s): 279d6aa
Files changed (2) hide show
  1. app.py +37 -21
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,12 +1,21 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
9
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -15,35 +24,42 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
 
 
 
 
 
38
 
39
- response += token
40
- yield response
41
 
42
  """
43
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
  """
45
  demo = gr.ChatInterface(
46
  respond,
 
47
  additional_inputs=[
48
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
1
  import gradio as gr
2
+ import spaces
3
  from huggingface_hub import InferenceClient
4
+ import torch
5
+ from transformers import AutoModelForCausalLM, ChameleonProcessor, AutoTokenizer, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
6
+ from threading import Thread
7
+ from PIL import Image
8
+ import requests
9
 
 
 
 
 
10
 
11
+ model_path = "facebook/chameleon-7b"
12
+ # model = ChameleonForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map="auto")
13
+ # processor = ChameleonProcessor.from_pretrained(model_path)
14
+ model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map="auto")
15
+ processor = ChameleonProcessor.from_pretrained(model_path)
16
+ tokenizer = processor.tokenizer
17
 
18
+ @spaces.GPU
19
  def respond(
20
  message,
21
  history: list[tuple[str, str]],
 
24
  temperature,
25
  top_p,
26
  ):
27
+ # messages = [{"role": "system", "content": system_message}]
28
 
29
+ # for val in history:
30
+ # if val[0]:
31
+ # messages.append({"role": "user", "content": val[0]})
32
+ # if val[1]:
33
+ # messages.append({"role": "assistant", "content": val[1]})
34
 
35
+ # messages.append({"role": "user", "content": message})
36
 
37
  response = ""
38
 
39
+ prompt = "I'm very intrigued by this work of art:<image>Please tell me about the artist."
40
+ image = Image.open(requests.get("https://uploads4.wikiart.org/images/paul-klee/death-for-the-idea-1915.jpg!Large.jpg", stream=True).raw)
41
+
42
+ inputs = processor(prompt, images=[image], return_tensors="pt").to(model.device, dtype=torch.bfloat16)
43
+ # out = model.generate(**inputs, max_new_tokens=40, do_sample=False)
44
+
45
+ streamer = TextIteratorStreamer(tokenizer)
46
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=20)
47
+
48
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
49
+ thread.start()
50
+
51
+ partial_message = ""
52
+ for new_token in streamer:
53
+ partial_message += new_token
54
+ yield partial_message
55
 
 
 
56
 
57
  """
58
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
59
  """
60
  demo = gr.ChatInterface(
61
  respond,
62
+ multimodal=True,
63
  additional_inputs=[
64
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
65
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- huggingface_hub==0.22.2
 
 
 
1
+ huggingface_hub>=0.23.2
2
+ git+https://github.com/huggingface/transformers.git@chameleon
3
+ accelerate