jonaschua commited on
Commit
11c1c09
·
verified ·
1 Parent(s): 4962fc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -20
app.py CHANGED
@@ -1,21 +1,45 @@
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]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  for val in history:
21
  if val[0]:
@@ -27,24 +51,23 @@ def respond(
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
  """
44
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
  """
 
 
46
  demo = gr.ChatInterface(
47
  respond,
 
 
 
48
  additional_inputs=[
49
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import spaces
4
+ import torch
5
+ import os
6
+ from huggingface_hub import login
7
+ from PIL import Image
8
 
9
  """
10
  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
11
  """
12
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
13
 
14
+ duration=None
15
 
16
+ login(token = os.getenv('deepseekv2'))
17
+
18
+ ckpt = "google/gemma-3-4b-it"
19
+ model = Gemma3ForConditionalGeneration.from_pretrained(
20
+ ckpt, device_map="auto", torch_dtype=torch.bfloat16,
21
+ )
22
+ processor = AutoProcessor.from_pretrained(ckpt)
23
+
24
+ # image = Image.open(requests.get(url, stream=True).raw)
25
+ # prompt = "<start_of_image> in this image, there is"
26
+ # model_inputs = processor(text=prompt, images=image, return_tensors="pt")
27
+ # input_len = model_inputs["input_ids"].shape[-1]
28
+
29
+ # with torch.inference_mode():
30
+ # generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
31
+ # generation = generation[0][input_len:]
32
+
33
+ @spaces.GPU(duration=duration)
34
+ def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p,):
35
+ # messages = [{"role": "system", "content": system_message}]
36
+
37
+ messages = [{
38
+ "role": "user",
39
+ "content": [
40
+ {"type": "image", "url": "https://huggingface.co/spaces/big-vision/paligemma-hf/resolve/main/examples/password.jpg"},
41
+ {"type": "text", "text": "What is the password?"}
42
+ ]}]
43
 
44
  for val in history:
45
  if val[0]:
 
51
 
52
  response = ""
53
 
54
+ # for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p,):
55
+ # token = message.choices[0].delta.content
 
 
 
 
 
 
56
 
57
+ # response += token
58
+ # yield response
59
 
60
 
61
  """
62
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
63
  """
64
+
65
+
66
  demo = gr.ChatInterface(
67
  respond,
68
+ textbox=gr.MultimodalTextbox()
69
+ multimodal=True,
70
+ stop_btn="Stop generation",
71
  additional_inputs=[
72
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
73
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),