daniloedu commited on
Commit
6f6480f
Β·
1 Parent(s): 3f4150d

Update the logic to comply with Llama 2

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -1,28 +1,33 @@
1
  import os
2
- import requests
3
- import gradio as gr
4
  from dotenv import load_dotenv, find_dotenv
5
- from transformers import pipeline
 
6
 
7
  # Load environment variables
8
  _ = load_dotenv(find_dotenv())
9
  hf_api_key = os.environ['HF_API_KEY']
10
 
11
  class Client:
12
- def __init__(self, base_url, headers, timeout):
13
- self.base_url = base_url
14
- self.headers = headers
15
- self.timeout = timeout
16
-
17
- def generate(self, prompt, max_new_tokens):
18
- # Your text generation code here.
19
- return {'generated_text': 'Placeholder text'}
20
-
 
21
  def generate_stream(self, prompt, max_new_tokens, stop_sequences, temperature):
22
- # Your streaming text generation code here.
23
- pass
 
 
 
 
24
 
25
- client = Client(os.environ['HF_API_FALCOM_BASE'], headers={"Authorization": f"Basic {hf_api_key}"}, timeout=120)
26
 
27
  def format_chat_prompt(message, chat_history, instruction):
28
  prompt = f"System:{instruction}"
@@ -35,20 +40,11 @@ def format_chat_prompt(message, chat_history, instruction):
35
  def respond(message, chat_history, instruction, temperature=0.7):
36
  prompt = format_chat_prompt(message, chat_history, instruction)
37
  chat_history = chat_history + [[message, ""]]
38
- stream = client.generate_stream(prompt, max_new_tokens=1024, stop_sequences=["\nUser:", ""], temperature=temperature)
39
- acc_text = ""
40
- for idx, response in enumerate(stream):
41
- text_token = response.token.text
42
- if response.details:
43
- return
44
- if idx == 0 and text_token.startswith(" "):
45
- text_token = text_token[1:]
46
- acc_text += text_token
47
- last_turn = list(chat_history.pop(-1))
48
- last_turn[-1] += acc_text
49
- chat_history = chat_history + [last_turn]
50
- yield "", chat_history
51
- acc_text = ""
52
 
53
  iface = gr.Interface(fn=respond, inputs=[gr.Textbox(label="Prompt"), gr.Chatbot(label="Chat History", height=240), gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."), gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)], outputs=[gr.Textbox(label="Prompt"), gr.Chatbot(label="Chat History", height=240)])
54
 
 
1
  import os
2
+ import torch
 
3
  from dotenv import load_dotenv, find_dotenv
4
+ import gradio as gr
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
6
 
7
  # Load environment variables
8
  _ = load_dotenv(find_dotenv())
9
  hf_api_key = os.environ['HF_API_KEY']
10
 
11
  class Client:
12
+ def __init__(self, model_name, device_map="auto"):
13
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ self.model = AutoModelForCausalLM.from_pretrained(
15
+ model_name,
16
+ device_map=device_map,
17
+ torch_dtype=torch.float16,
18
+ load_in_8bit=True,
19
+ rope_scaling={"type": "dynamic", "factor": 2}
20
+ )
21
+
22
  def generate_stream(self, prompt, max_new_tokens, stop_sequences, temperature):
23
+ inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
24
+ del inputs["token_type_ids"]
25
+ streamer = TextStreamer(self.tokenizer, skip_prompt=True, skip_special_tokens=True)
26
+ output = self.model.generate(**inputs, streamer=streamer, use_cache=True, max_new_tokens=float('inf'))
27
+ output_text = self.tokenizer.decode(output[0], skip_special_tokens=True)
28
+ return output_text
29
 
30
+ client = Client("upstage/Llama-2-70b-instruct")
31
 
32
  def format_chat_prompt(message, chat_history, instruction):
33
  prompt = f"System:{instruction}"
 
40
  def respond(message, chat_history, instruction, temperature=0.7):
41
  prompt = format_chat_prompt(message, chat_history, instruction)
42
  chat_history = chat_history + [[message, ""]]
43
+ output_text = client.generate_stream(prompt, max_new_tokens=1024, stop_sequences=["\nUser:", ""], temperature=temperature)
44
+ last_turn = list(chat_history.pop(-1))
45
+ last_turn[-1] += output_text
46
+ chat_history = chat_history + [last_turn]
47
+ return "", chat_history
 
 
 
 
 
 
 
 
 
48
 
49
  iface = gr.Interface(fn=respond, inputs=[gr.Textbox(label="Prompt"), gr.Chatbot(label="Chat History", height=240), gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."), gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)], outputs=[gr.Textbox(label="Prompt"), gr.Chatbot(label="Chat History", height=240)])
50