Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,59 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
model = AutoModelForCausalLM.from_pretrained(
|
20 |
-
MODEL_ID,
|
21 |
-
device_map="auto", # << key change
|
22 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
23 |
-
low_cpu_mem_usage=True,
|
24 |
-
token=HF_TOKEN,
|
25 |
-
)
|
26 |
-
|
27 |
-
# Ensure pad token is set
|
28 |
-
if tokenizer.pad_token_id is None and tokenizer.eos_token_id is not None:
|
29 |
-
tokenizer.pad_token_id = tokenizer.eos_token_id
|
30 |
-
|
31 |
-
|
32 |
-
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
33 |
-
# Build chat messages
|
34 |
messages = [{"role": "system", "content": system_message}]
|
35 |
-
for
|
36 |
-
if
|
37 |
-
messages.append({"role": "user", "content":
|
38 |
-
if
|
39 |
-
messages.append({"role": "assistant", "content":
|
40 |
messages.append({"role": "user", "content": message})
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
"streamer": streamer,
|
62 |
-
}
|
63 |
-
|
64 |
-
thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
|
65 |
-
thread.start()
|
66 |
-
|
67 |
-
partial = ""
|
68 |
-
for new_text in streamer:
|
69 |
-
partial += new_text
|
70 |
-
yield partial
|
71 |
-
|
72 |
-
|
73 |
-
# ---- Gradio UI ----
|
74 |
demo = gr.ChatInterface(
|
75 |
respond,
|
76 |
additional_inputs=[
|
77 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
78 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
79 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
80 |
-
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
],
|
82 |
)
|
83 |
|
@@ -85,3 +61,4 @@ if __name__ == "__main__":
|
|
85 |
demo.launch()
|
86 |
|
87 |
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from witness.witness_rzero import WitnessRZero
|
3 |
+
import app_math as app_math
|
4 |
+
|
5 |
+
# Instantiate WitnessRZero – change device to "cuda" if GPU is available
|
6 |
+
wrz = WitnessRZero(device="cpu")
|
7 |
+
|
8 |
+
def respond(
|
9 |
+
message,
|
10 |
+
history: list[tuple[str, str]],
|
11 |
+
system_message,
|
12 |
+
max_tokens,
|
13 |
+
temperature,
|
14 |
+
top_p,
|
15 |
+
):
|
16 |
+
# Build conversation history in OpenAI-style message format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
messages = [{"role": "system", "content": system_message}]
|
18 |
+
for val in history:
|
19 |
+
if val[0]:
|
20 |
+
messages.append({"role": "user", "content": val[0]})
|
21 |
+
if val[1]:
|
22 |
+
messages.append({"role": "assistant", "content": val[1]})
|
23 |
messages.append({"role": "user", "content": message})
|
24 |
|
25 |
+
# Concatenate all into a single prompt for WitnessRZero
|
26 |
+
prompt = ""
|
27 |
+
for m in messages:
|
28 |
+
prompt += f"{m['role'].capitalize()}: {m['content']}\n"
|
29 |
+
|
30 |
+
response = ""
|
31 |
+
# Stream the output from WitnessRZero.generate()
|
32 |
+
for token in wrz.client.text_generation(
|
33 |
+
prompt,
|
34 |
+
max_new_tokens=max_tokens,
|
35 |
+
stream=True,
|
36 |
+
temperature=temperature,
|
37 |
+
top_p=top_p,
|
38 |
+
):
|
39 |
+
part = token # huggingface_hub’s stream yields token text chunks
|
40 |
+
response += part
|
41 |
+
yield response
|
42 |
+
|
43 |
+
# Build the Gradio ChatInterface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
demo = gr.ChatInterface(
|
45 |
respond,
|
46 |
additional_inputs=[
|
47 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
48 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
49 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
50 |
+
gr.Slider(
|
51 |
+
minimum=0.1,
|
52 |
+
maximum=1.0,
|
53 |
+
value=0.95,
|
54 |
+
step=0.05,
|
55 |
+
label="Top-p (nucleus sampling)",
|
56 |
+
),
|
57 |
],
|
58 |
)
|
59 |
|
|
|
61 |
demo.launch()
|
62 |
|
63 |
|
64 |
+
|