Akjava commited on
Commit
8aaa2cd
Β·
verified Β·
1 Parent(s): 6cfb433

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -0
app.py CHANGED
@@ -1,3 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import spaces
2
  import os
3
  import torch
 
1
+ #duplicated from https://huggingface.co/spaces/chheplo/DeepSeek-R1-Distill-Llama-8B
2
+ import gradio as gr
3
+ import os
4
+ import spaces
5
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
7
+ from threading import Thread
8
+
9
+ # Set an environment variable
10
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
11
+
12
+
13
+ DESCRIPTION = '''
14
+ <div>
15
+ <h1 style="text-align: center;">deepseek-ai/DeepSeek-R1-Distill-Llama-8B</h1>
16
+ </div>
17
+ '''
18
+
19
+ LICENSE = """
20
+ <p/>
21
+
22
+ ---
23
+ """
24
+
25
+ PLACEHOLDER = """
26
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
27
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">DeepSeek-R1-Distill-Llama-8B</h1>
28
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
29
+ </div>
30
+ """
31
+
32
+
33
+ css = """
34
+ h1 {
35
+ text-align: center;
36
+ display: block;
37
+ }
38
+
39
+ #duplicate-button {
40
+ margin: auto;
41
+ color: white;
42
+ background: #1565c0;
43
+ border-radius: 100vh;
44
+ }
45
+ """
46
+ model_id = "AXCXEPT/phi-4-deepseek-R1K-RL-EZO"
47
+ model_id = "AXCXEPT/phi-4-open-R1-Distill-EZOv1"
48
+
49
+ # Load the tokenizer and model
50
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
51
+ model = AutoModelForCausalLM.from_pretrained(model_id) # to("cuda:0")
52
+ terminators = [
53
+ tokenizer.eos_token_id,
54
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
55
+ ]
56
+
57
+ @spaces.GPU(duration=120)
58
+ def chat_llama3_8b(message: str,
59
+ history: list,
60
+ temperature: float,
61
+ max_new_tokens: int
62
+ ) -> str:
63
+ """
64
+ Generate a streaming response using the llama3-8b model.
65
+ Args:
66
+ message (str): The input message.
67
+ history (list): The conversation history used by ChatInterface.
68
+ temperature (float): The temperature for generating the response.
69
+ max_new_tokens (int): The maximum number of new tokens to generate.
70
+ Returns:
71
+ str: The generated response.
72
+ """
73
+ conversation = []
74
+ for user, assistant in history:
75
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
76
+ conversation.append({"role": "user", "content": message})
77
+
78
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
79
+
80
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
81
+
82
+ generate_kwargs = dict(
83
+ input_ids= input_ids,
84
+ streamer=streamer,
85
+ max_new_tokens=max_new_tokens,
86
+ do_sample=True,
87
+ temperature=temperature,
88
+ eos_token_id=terminators,
89
+ )
90
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
91
+ if temperature == 0:
92
+ generate_kwargs['do_sample'] = False
93
+
94
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
95
+ t.start()
96
+
97
+ outputs = []
98
+ for text in streamer:
99
+ outputs.append(text)
100
+ #print(outputs)
101
+ yield "".join(outputs)
102
+
103
+
104
+ # Gradio block
105
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
106
+
107
+ with gr.Blocks(fill_height=True, css=css) as demo:
108
+
109
+ gr.Markdown(DESCRIPTION)
110
+ gr.ChatInterface(
111
+ fn=chat_llama3_8b,
112
+ chatbot=chatbot,
113
+ fill_height=True,
114
+ additional_inputs_accordion=gr.Accordion(label="βš™οΈ Parameters", open=False, render=False),
115
+ additional_inputs=[
116
+ gr.Slider(minimum=0,
117
+ maximum=1,
118
+ step=0.1,
119
+ value=0.5,
120
+ label="Temperature",
121
+ render=False),
122
+ gr.Slider(minimum=128,
123
+ maximum=4096,
124
+ step=1,
125
+ value=1024,
126
+ label="Max new tokens",
127
+ render=False ),
128
+ ],
129
+ examples=[
130
+ ['How to setup a human base on Mars? Give short answer.'],
131
+ ['Explain theory of relativity to me like I’m 8 years old.'],
132
+ ['What is 9,000 * 9,000?'],
133
+ ['Write a pun-filled happy birthday message to my friend Alex.'],
134
+ ['Justify why a penguin might make a good king of the jungle.']
135
+ ],
136
+ cache_examples=False,
137
+ )
138
+
139
+ gr.Markdown(LICENSE)
140
+
141
+ if __name__ == "__main__":
142
+ demo.launch()
143
+
144
+
145
+
146
+
147
  import spaces
148
  import os
149
  import torch