ShravanHN commited on
Commit
6f77292
·
1 Parent(s): 2eaa8b8

initial commit

Browse files
Files changed (2) hide show
  1. app.py +132 -4
  2. requirements.txt +3 -0
app.py CHANGED
@@ -1,7 +1,135 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
+ import spaces
4
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
6
+ from threading import Thread
7
 
8
+ # Set an environment variable
9
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
10
 
11
+
12
+ DESCRIPTION = '''
13
+ <div>
14
+ <h1 style="text-align: center;">ContenteaseAI custom trained model</h1>
15
+ </div>
16
+ '''
17
+
18
+ LICENSE = """
19
+ <p/>
20
+
21
+ ---
22
+ Built with Dolphin Meta Llama 3
23
+ """
24
+
25
+ PLACEHOLDER = """
26
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
27
+ <img src="https://ysharma-dummy-chat-app.hf.space/file=/tmp/gradio/8e75e61cc9bab22b7ce3dec85ab0e6db1da5d107/Meta_lockup_positive%20primary_RGB.jpg" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
28
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Dolphin Meta llama3</h1>
29
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
30
+ </div>
31
+ """
32
+
33
+
34
+ css = """
35
+ h1 {
36
+ text-align: center;
37
+ display: block;
38
+ }
39
+
40
+ #duplicate-button {
41
+ margin: auto;
42
+ color: white;
43
+ background: #1565c0;
44
+ border-radius: 100vh;
45
+ }
46
+ """
47
+
48
+ # Load the tokenizer and model
49
+ tokenizer = AutoTokenizer.from_pretrained("cognitivecomputations/dolphin-2.9-llama3-8b")
50
+ model = AutoModelForCausalLM.from_pretrained("cognitivecomputations/dolphin-2.9-llama3-8b", device_map="auto") # to("cuda:0")
51
+ terminators = [
52
+ tokenizer.eos_token_id,
53
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
54
+ ]
55
+
56
+ @spaces.GPU(duration=120)
57
+ def chat_llama3_8b(message: str,
58
+ history: list,
59
+ temperature: float,
60
+ max_new_tokens: int
61
+ ) -> str:
62
+ """
63
+ Generate a streaming response using the llama3-8b model.
64
+ Args:
65
+ message (str): The input message.
66
+ history (list): The conversation history used by ChatInterface.
67
+ temperature (float): The temperature for generating the response.
68
+ max_new_tokens (int): The maximum number of new tokens to generate.
69
+ Returns:
70
+ str: The generated response.
71
+ """
72
+ conversation = []
73
+ for user, assistant in history:
74
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
75
+ conversation.append({"role": "user", "content": message})
76
+
77
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
78
+
79
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
80
+
81
+ generate_kwargs = dict(
82
+ input_ids= input_ids,
83
+ streamer=streamer,
84
+ max_new_tokens=max_new_tokens,
85
+ do_sample=True,
86
+ temperature=temperature,
87
+ eos_token_id=terminators,
88
+ )
89
+ if temperature == 0:
90
+ generate_kwargs['do_sample'] = False
91
+
92
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
93
+ t.start()
94
+
95
+ outputs = []
96
+ for text in streamer:
97
+ outputs.append(text)
98
+ #print(outputs)
99
+ yield "".join(outputs)
100
+
101
+
102
+ # Gradio block
103
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
104
+
105
+ with gr.Blocks(fill_height=True, css=css) as demo:
106
+
107
+ gr.Markdown(DESCRIPTION)
108
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
109
+ gr.ChatInterface(
110
+ fn=chat_llama3_8b,
111
+ chatbot=chatbot,
112
+ fill_height=True,
113
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
114
+ additional_inputs=[
115
+ gr.Slider(minimum=0,
116
+ maximum=1,
117
+ step=0.1,
118
+ value=0.95,
119
+ label="Temperature",
120
+ render=False),
121
+ gr.Slider(minimum=128,
122
+ maximum=9012,
123
+ step=1,
124
+ value=512,
125
+ label="Max new tokens",
126
+ render=False ),
127
+ ],
128
+
129
+ )
130
+
131
+ gr.Markdown(LICENSE)
132
+
133
+ if __name__ == "__main__":
134
+ demo.launch()
135
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ accelerate
2
+ transformers
3
+ SentencePiece