eswardivi commited on
Commit
63b82b4
1 Parent(s): f5167b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -20
app.py CHANGED
@@ -1,6 +1,11 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
 
 
 
 
 
4
  import os
5
  from threading import Thread
6
  import spaces
@@ -9,27 +14,27 @@ import time
9
  token = os.environ["HF_TOKEN"]
10
 
11
  quantization_config = BitsAndBytesConfig(
12
- load_in_4bit=True,
13
- bnb_4bit_compute_dtype=torch.float16
14
  )
15
 
16
- model = AutoModelForCausalLM.from_pretrained("google/gemma-1.1-7b-it",
17
- quantization_config=quantization_config,
18
- token=token)
19
  tok = AutoTokenizer.from_pretrained("google/gemma-1.1-7b-it", token=token)
20
 
21
  if torch.cuda.is_available():
22
- device = torch.device('cuda')
23
  print(f"Using GPU: {torch.cuda.get_device_name(device)}")
24
  else:
25
- device = torch.device('cpu')
26
  print("Using CPU")
27
-
28
  # model = model.to(device)
29
- # Dispatch Errors
 
30
 
31
  @spaces.GPU
32
- def chat(message, history):
33
  start_time = time.time()
34
  chat = []
35
  for item in history:
@@ -40,16 +45,16 @@ def chat(message, history):
40
  messages = tok.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
41
  model_inputs = tok([messages], return_tensors="pt").to(device)
42
  streamer = TextIteratorStreamer(
43
- tok, timeout=10., skip_prompt=True, skip_special_tokens=True)
 
44
  generate_kwargs = dict(
45
  model_inputs,
46
  streamer=streamer,
47
- max_new_tokens=1024,
48
  do_sample=True,
49
- top_p=0.95,
50
- top_k=1000,
51
- temperature=0.75,
52
- num_beams=1,
53
  )
54
  t = Thread(target=model.generate, kwargs=generate_kwargs)
55
  t.start()
@@ -66,9 +71,36 @@ def chat(message, history):
66
  tokens = len(tok.tokenize(partial_text))
67
  tokens_per_second = tokens / total_time if total_time > 0 else 0
68
 
69
- # Append the timing information to the final output
70
- timing_info = f"\nTime taken to first token: {first_token_time:.2f} seconds\nTokens per second: {tokens_per_second:.2f}"
71
  yield partial_text + timing_info
72
 
73
- demo = gr.ChatInterface(fn=chat, examples=[["Write me a poem about Machine Learning."]], title="Chat With LLMS")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  demo.launch()
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import (
4
+ AutoModelForCausalLM,
5
+ AutoTokenizer,
6
+ TextIteratorStreamer,
7
+ BitsAndBytesConfig,
8
+ )
9
  import os
10
  from threading import Thread
11
  import spaces
 
14
  token = os.environ["HF_TOKEN"]
15
 
16
  quantization_config = BitsAndBytesConfig(
17
+ load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16
 
18
  )
19
 
20
+ model = AutoModelForCausalLM.from_pretrained(
21
+ "google/gemma-1.1-7b-it", quantization_config=quantization_config, token=token
22
+ )
23
  tok = AutoTokenizer.from_pretrained("google/gemma-1.1-7b-it", token=token)
24
 
25
  if torch.cuda.is_available():
26
+ device = torch.device("cuda")
27
  print(f"Using GPU: {torch.cuda.get_device_name(device)}")
28
  else:
29
+ device = torch.device("cpu")
30
  print("Using CPU")
31
+
32
  # model = model.to(device)
33
+ # Dispatch Errors
34
+
35
 
36
  @spaces.GPU
37
+ def chat(message, history, temperature, top_p, top_k, max_tokens):
38
  start_time = time.time()
39
  chat = []
40
  for item in history:
 
45
  messages = tok.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
46
  model_inputs = tok([messages], return_tensors="pt").to(device)
47
  streamer = TextIteratorStreamer(
48
+ tok, timeout=10.0, skip_prompt=True, skip_special_tokens=True
49
+ )
50
  generate_kwargs = dict(
51
  model_inputs,
52
  streamer=streamer,
53
+ max_new_tokens=max_tokens,
54
  do_sample=True,
55
+ top_p=top_p,
56
+ top_k=top_k,
57
+ temperature=temperature,
 
58
  )
59
  t = Thread(target=model.generate, kwargs=generate_kwargs)
60
  t.start()
 
71
  tokens = len(tok.tokenize(partial_text))
72
  tokens_per_second = tokens / total_time if total_time > 0 else 0
73
 
74
+ timing_info = f"\n\nTime taken to first token: {first_token_time:.2f} seconds\nTokens per second: {tokens_per_second:.2f}"
 
75
  yield partial_text + timing_info
76
 
77
+
78
+ demo = gr.ChatInterface(
79
+ fn=chat,
80
+ examples=[["Write me a poem about Machine Learning."]],
81
+ additional_inputs_accordion=gr.Accordion(
82
+ label="⚙️ Parameters", open=False, render=False
83
+ ),
84
+ additional_inputs=[
85
+ gr.Slider(
86
+ minimum=0, maximum=1, step=0.1, value=0.9, label="Temperature", render=False
87
+ ),
88
+ gr.Slider(
89
+ minimum=0, maximum=1, step=0.1, value=0.95, label="top_p", render=False
90
+ ),
91
+ gr.Slider(
92
+ minimum=1, maximum=10000, step=5, value=1000, label="top_k", render=False
93
+ ),
94
+ gr.Slider(
95
+ minimum=128,
96
+ maximum=4096,
97
+ step=1,
98
+ value=1024,
99
+ label="Max new tokens",
100
+ render=False,
101
+ ),
102
+ ],
103
+ multimodal=False,
104
+ title="Chat With LLMs",
105
+ )
106
  demo.launch()