Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,53 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from witness.witness_rzero import WitnessRZero
|
| 3 |
import app_math as app_math
|
| 4 |
|
| 5 |
-
|
| 6 |
-
wrz = WitnessRZero(device="cpu")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
):
|
| 16 |
-
# Build conversation history in OpenAI-style message format
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
-
for
|
| 19 |
-
if
|
| 20 |
-
|
| 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 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
prompt,
|
| 34 |
max_new_tokens=max_tokens,
|
| 35 |
stream=True,
|
| 36 |
temperature=temperature,
|
| 37 |
top_p=top_p,
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 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=
|
| 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 |
|
|
@@ -62,3 +56,4 @@ if __name__ == "__main__":
|
|
| 62 |
|
| 63 |
|
| 64 |
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
import gradio as gr
|
| 3 |
from witness.witness_rzero import WitnessRZero
|
| 4 |
import app_math as app_math
|
| 5 |
|
| 6 |
+
wrz = WitnessRZero(device="cpu") # remote inference speed dominates anyway
|
|
|
|
| 7 |
|
| 8 |
+
HISTORY_TURNS = 3 # keep only last N turns
|
| 9 |
+
PROMPT_CHAR_BUDGET = 6000 # trim long contexts
|
| 10 |
+
GENERATION_TIME_CAP_S = 20 # stop streaming after N seconds
|
| 11 |
+
|
| 12 |
+
def build_prompt(message, history, system_message):
|
| 13 |
+
# keep only the last HISTORY_TURNS exchanges
|
| 14 |
+
short_hist = history[-HISTORY_TURNS:] if history else []
|
|
|
|
|
|
|
| 15 |
messages = [{"role": "system", "content": system_message}]
|
| 16 |
+
for u, a in short_hist:
|
| 17 |
+
if u: messages.append({"role": "user", "content": u})
|
| 18 |
+
if a: messages.append({"role": "assistant", "content": a})
|
|
|
|
|
|
|
| 19 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
| 20 |
prompt = ""
|
| 21 |
for m in messages:
|
| 22 |
prompt += f"{m['role'].capitalize()}: {m['content']}\n"
|
| 23 |
+
# hard trim to avoid token explosion
|
| 24 |
+
return prompt[-PROMPT_CHAR_BUDGET:]
|
| 25 |
|
| 26 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 27 |
+
prompt = build_prompt(message, history, system_message)
|
| 28 |
+
response, start = "", time.time()
|
| 29 |
+
stream = wrz.client.text_generation(
|
| 30 |
prompt,
|
| 31 |
max_new_tokens=max_tokens,
|
| 32 |
stream=True,
|
| 33 |
temperature=temperature,
|
| 34 |
top_p=top_p,
|
| 35 |
+
# return_full_text=False # set this in WitnessRZero if available
|
| 36 |
+
)
|
| 37 |
+
for part in stream:
|
| 38 |
response += part
|
| 39 |
yield response
|
| 40 |
+
if time.time() - start > GENERATION_TIME_CAP_S:
|
| 41 |
+
yield response + "\n\n[stopped for speed; try 'Max new tokens' higher or ask a smaller question]"
|
| 42 |
+
return
|
| 43 |
|
|
|
|
| 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=128, 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(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
],
|
| 52 |
)
|
| 53 |
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
+
|