coeuslearning commited on
Commit
b644449
1 Parent(s): bae1d90

Create appbkup.py

Browse files
Files changed (1) hide show
  1. appbkup.py +90 -0
appbkup.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from threading import Thread
3
+ from typing import Iterator
4
+
5
+ import gradio as gr
6
+ import spaces
7
+ import torch
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
9
+
10
+ HF_TOKEN = "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" ##Llama Write Token
11
+
12
+ MAX_MAX_NEW_TOKENS = 2048
13
+ DEFAULT_MAX_NEW_TOKENS = 1024
14
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
15
+
16
+ DESCRIPTION = """\
17
+ # Llama. Protected. With Protecto.
18
+ """
19
+
20
+ if not torch.cuda.is_available():
21
+ DESCRIPTION += "\n<p>Running on CPU. Please enable GPU</p>"
22
+
23
+
24
+ if torch.cuda.is_available():
25
+ model_id = "meta-llama/Llama-2-7b-chat-hf"
26
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto", use_auth_token=HF_TOKEN)
27
+ tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
28
+ tokenizer.use_default_system_prompt = False
29
+
30
+ @spaces.GPU
31
+ def generate(
32
+ message: str,
33
+ chat_history: list[tuple[str, str]],
34
+ system_prompt: str,
35
+ max_new_tokens: int = 1024,
36
+ temperature: float = 0.6,
37
+ top_p: float = 0.9,
38
+ top_k: int = 50,
39
+ repetition_penalty: float = 1.2,
40
+ ) -> Iterator[str]:
41
+ conversation = []
42
+ if system_prompt:
43
+ conversation.append({"role": "system", "content": system_prompt})
44
+ for user, assistant in chat_history:
45
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
46
+ conversation.append({"role": "user", "content": message})
47
+
48
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
49
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
50
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
51
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
52
+ input_ids = input_ids.to(model.device)
53
+
54
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
55
+ generate_kwargs = dict(
56
+ {"input_ids": input_ids},
57
+ streamer=streamer,
58
+ max_new_tokens=max_new_tokens,
59
+ do_sample=True,
60
+ top_p=top_p,
61
+ top_k=top_k,
62
+ temperature=temperature,
63
+ num_beams=1,
64
+ repetition_penalty=repetition_penalty,
65
+ )
66
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
67
+ t.start()
68
+
69
+ outputs = []
70
+ for text in streamer:
71
+ outputs.append(text)
72
+ yield "".join(outputs)
73
+
74
+
75
+ chat_interface = gr.ChatInterface(
76
+ fn=generate,
77
+ additional_inputs=[
78
+ gr.Textbox(label="System prompt", lines=6),
79
+ ],
80
+ retry_btn=None,
81
+ stop_btn=None,
82
+ undo_btn=None,
83
+ clear_btn=None,
84
+ )
85
+ with gr.Blocks(css="style.css") as demo:
86
+ gr.Markdown(DESCRIPTION)
87
+ chat_interface.render()
88
+
89
+ if __name__ == "__main__":
90
+ demo.queue(max_size=20).launch()