Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
from huggingface_hub import InferenceClient
|
| 6 |
-
|
| 7 |
-
# Read token from Space secrets
|
| 8 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
-
|
| 10 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
|
| 11 |
-
|
| 12 |
-
"""
|
| 13 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 14 |
-
"""
|
| 15 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def respond(
|
| 19 |
-
message,
|
| 20 |
-
history: list[tuple[str, str]],
|
| 21 |
-
system_message,
|
| 22 |
-
max_tokens,
|
| 23 |
-
temperature,
|
| 24 |
-
top_p,
|
| 25 |
-
):
|
| 26 |
-
messages = [{"role": "system", "content": system_message}]
|
| 27 |
-
|
| 28 |
-
for val in history:
|
| 29 |
-
if val[0]:
|
| 30 |
-
messages.append({"role": "user", "content": val[0]})
|
| 31 |
-
if val[1]:
|
| 32 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 33 |
-
|
| 34 |
-
messages.append({"role": "user", "content": message})
|
| 35 |
-
|
| 36 |
-
response = ""
|
| 37 |
-
|
| 38 |
-
for message in client.chat_completion(
|
| 39 |
-
messages,
|
| 40 |
-
max_tokens=max_tokens,
|
| 41 |
-
stream=True,
|
| 42 |
-
temperature=temperature,
|
| 43 |
-
top_p=top_p,
|
| 44 |
-
):
|
| 45 |
-
token = message.choices[0].delta.content
|
| 46 |
-
|
| 47 |
-
response += token
|
| 48 |
-
yield response
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
"""
|
| 52 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 53 |
-
"""
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
-
|
| 56 |
additional_inputs=[
|
| 57 |
-
gr.
|
| 58 |
-
gr.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
value=0.95,
|
| 64 |
-
step=0.05,
|
| 65 |
-
label="Top-p (nucleus sampling)",
|
| 66 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
demo.launch()
|
|
|
|
| 1 |
+
3.85 kB
|
| 2 |
+
from transformers import pipeline, TextIteratorStreamer
|
| 3 |
+
import torch
|
| 4 |
+
from threading import Thread
|
| 5 |
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
+
model_id = "openai/gpt-oss-20b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
pipe = pipeline(
|
| 12 |
+
"text-generation",
|
| 13 |
+
model=model_id,
|
| 14 |
+
torch_dtype="auto",
|
| 15 |
+
device_map="auto",
|
| 16 |
+
)
|
| 17 |
+
def format_conversation_history(chat_history):
|
| 18 |
+
messages = []
|
| 19 |
+
for item in chat_history:
|
| 20 |
+
role = item["role"]
|
| 21 |
+
content = item["content"]
|
| 22 |
+
if isinstance(content, list):
|
| 23 |
+
content = content[0]["text"] if content and "text" in content[0] else str(content)
|
| 24 |
+
messages.append({"role": role, "content": content})
|
| 25 |
+
return messages
|
| 26 |
+
|
| 27 |
+
@spaces.GPU()
|
| 28 |
+
def generate_response(input_data, chat_history, max_new_tokens, system_prompt, temperature, top_p, top_k, repetition_penalty):
|
| 29 |
+
new_message = {"role": "user", "content": input_data}
|
| 30 |
+
system_message = [{"role": "system", "content": system_prompt}] if system_prompt else []
|
| 31 |
+
processed_history = format_conversation_history(chat_history)
|
| 32 |
+
messages = system_message + processed_history + [new_message]
|
| 33 |
+
streamer = TextIteratorStreamer(pipe.tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 34 |
+
generation_kwargs = {
|
| 35 |
+
"max_new_tokens": max_new_tokens,
|
| 36 |
+
"do_sample": True,
|
| 37 |
+
"temperature": temperature,
|
| 38 |
+
"top_p": top_p,
|
| 39 |
+
"top_k": top_k,
|
| 40 |
+
"repetition_penalty": repetition_penalty,
|
| 41 |
+
"streamer": streamer
|
| 42 |
+
}
|
| 43 |
+
thread = Thread(target=pipe, args=(messages,), kwargs=generation_kwargs)
|
| 44 |
+
thread.start()
|
| 45 |
+
# simple formatting without harmony because of no tool usage etc. and experienced hf space problems with harmony
|
| 46 |
+
thinking = ""
|
| 47 |
+
final = ""
|
| 48 |
+
started_final = False
|
| 49 |
+
for chunk in streamer:
|
| 50 |
+
if not started_final:
|
| 51 |
+
if "assistantfinal" in chunk.lower():
|
| 52 |
+
split_parts = re.split(r'assistantfinal', chunk, maxsplit=1)
|
| 53 |
+
thinking += split_parts[0]
|
| 54 |
+
final += split_parts[1]
|
| 55 |
+
started_final = True
|
| 56 |
+
else:
|
| 57 |
+
thinking += chunk
|
| 58 |
+
else:
|
| 59 |
+
final += chunk
|
| 60 |
+
clean_thinking = re.sub(r'^analysis\s*', '', thinking).strip()
|
| 61 |
+
clean_final = final.strip()
|
| 62 |
+
formatted = f"<details open><summary>Click to view Thinking Process</summary>\n\n{clean_thinking}\n\n</details>\n\n{clean_final}"
|
| 63 |
+
yield formatted
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
demo = gr.ChatInterface(
|
| 66 |
+
fn=generate_response,
|
| 67 |
additional_inputs=[
|
| 68 |
+
gr.Slider(label="Max new tokens", minimum=64, maximum=4096, step=1, value=2048),
|
| 69 |
+
gr.Textbox(
|
| 70 |
+
label="System Prompt",
|
| 71 |
+
value="You are a helpful assistant. Reasoning: medium",
|
| 72 |
+
lines=4,
|
| 73 |
+
placeholder="Change system prompt"
|
|
|
|
|
|
|
|
|
|
| 74 |
),
|
| 75 |
+
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
|
| 76 |
+
gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
|
| 77 |
+
gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=50),
|
| 78 |
+
gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
|
| 79 |
+
],
|
| 80 |
+
examples=[
|
| 81 |
+
[{"text": "Explain Newton laws clearly and concisely"}],
|
| 82 |
+
[{"text": "Write a Python function to calculate the Fibonacci sequence"}],
|
| 83 |
+
[{"text": "What are the benefits of open weight AI models"}],
|
| 84 |
],
|
| 85 |
+
cache_examples=False,
|
| 86 |
+
type="messages",
|
| 87 |
+
description="""# gpt-oss-20b Demo
|
| 88 |
+
Give it a couple of seconds to start. You can adjust reasoning level in the system prompt like "Reasoning: high." Click to view thinking process (default is on).""",
|
| 89 |
+
fill_height=True,
|
| 90 |
+
textbox=gr.Textbox(
|
| 91 |
+
label="Query Input",
|
| 92 |
+
placeholder="Type your prompt"
|
| 93 |
+
),
|
| 94 |
+
stop_btn="Stop Generation",
|
| 95 |
+
multimodal=False,
|
| 96 |
+
theme=gr.themes.Soft()
|
| 97 |
)
|
| 98 |
|
|
|
|
| 99 |
if __name__ == "__main__":
|
| 100 |
+
demo.launch(share=True)
|