valentin urena commited on
Commit
2b49a9b
·
verified ·
1 Parent(s): b7ab4ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -44
app.py CHANGED
@@ -1,64 +1,104 @@
 
 
 
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]:
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
  """
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"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
  ),
59
  ],
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
+ os.environ["KERAS_BACKEND"] = "torch" # "jax", "torch" or "tensorflow"
3
+
4
  import gradio as gr
5
+ import keras_nlp
6
+ import keras
7
+ import spaces
8
+ import torch
9
 
10
+ from typing import Iterator
11
+ # import time
 
 
12
 
13
+ # from chess_board import Game
14
 
 
 
 
 
 
 
 
 
 
15
 
16
+ print(f"Is CUDA available: {torch.cuda.is_available()}")
17
+ print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
 
 
 
18
 
19
+ MAX_INPUT_TOKEN_LENGTH = 4096
20
 
21
+ MAX_NEW_TOKENS = 2048
22
+ DEFAULT_MAX_NEW_TOKENS = 128
23
 
24
+ model_id = "hf://google/gemma-2b-keras"
 
 
 
 
 
 
 
25
 
 
 
26
 
27
+ model = keras_nlp.models.GemmaCausalLM.from_preset(model_id)
28
+ tokenizer = model.preprocessor.tokenizer
29
 
30
+ DESCRIPTION = """
31
+ # Gemma 2B
32
+ **Welcome to the Gemma Chess Chatbot!**
33
+
34
+ This game mode allows you to play a game against Gemma, the input must be in algebraic notation. \n
35
+ If you need help learning algebraic notation ask Gemma!
36
  """
37
+
38
+ # @spaces.GPU
39
+ def generate(
40
+ message: str,
41
+ chat_history: list[dict],
42
+ max_new_tokens: int = 1024,
43
+ ) -> Iterator[str]:
44
+
45
+ input_ids = tokenizer.tokenize(message)
46
+
47
+ if len(input_ids) > MAX_INPUT_TOKEN_LENGTH:
48
+ input_ids = input_ids[-MAX_INPUT_TOKEN_LENGTH:]
49
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
50
+
51
+ response = model.generate(message, max_length=max_new_tokens)
52
+
53
+ outputs = ""
54
+
55
+ for char in response:
56
+ outputs += char
57
+ yield outputs
58
+
59
+
60
+ chat_interface = gr.ChatInterface(
61
+ fn=generate,
62
  additional_inputs=[
 
 
 
63
  gr.Slider(
64
+ label="Max new tokens",
65
+ minimum=1,
66
+ maximum=MAX_NEW_TOKENS,
67
+ step=1,
68
+ value=DEFAULT_MAX_NEW_TOKENS,
69
  ),
70
  ],
71
+ stop_btn=None,
72
+ examples=[
73
+ ["Hello there! How are you doing?"],
74
+ ["Can you explain briefly to me what is the Python programming language?"],
75
+ ["Explain the plot of Cinderella in a sentence."],
76
+ ["How many hours does it take a man to eat a Helicopter?"],
77
+ ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
78
+ ],
79
+ cache_examples=False,
80
+ type="messages",
81
  )
82
 
83
+ with gr.Blocks(css_paths="./style.css", fill_height=True) as demo:
84
+ gr.Markdown(DESCRIPTION)
85
+
86
+ play_match = Game()
87
+
88
+ # chess_png = gr.Image(play_match.display_board())
89
+ with gr.Row():
90
+ board_image = gr.HTML(play_match.display_board())
91
+ with gr.Column():
92
+ chat_interface.render()
93
+
94
+ move_input = gr.Textbox(label="Enter your move in algebraic notation (e.g., e4, Nf3, Bxc4)")
95
+
96
+ btn = gr.Button("Submit Move")
97
+ btn.click(play_match.generate_moves, inputs=move_input, outputs=board_image)
98
+
99
+ reset_btn = gr.Button("Reset Game")
100
+ reset_btn.click(play_match.reset_board, outputs=board_image)
101
+
102
 
103
  if __name__ == "__main__":
104
+ demo.queue(max_size=20).launch()