Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,4 +73,84 @@ def respond(
|
|
| 73 |
|
| 74 |
outputs = pipe(
|
| 75 |
prompt,
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
outputs = pipe(
|
| 75 |
prompt,
|
| 76 |
+
max_new_tokens=max_tokens,
|
| 77 |
+
do_sample=True,
|
| 78 |
+
temperature=temperature,
|
| 79 |
+
top_p=top_p,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
response = outputs[0]["generated_text"][len(prompt):]
|
| 83 |
+
yield response.strip()
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
print("[MODE] api")
|
| 87 |
+
# First try logged-in user token, then Space-level HF_TOKEN
|
| 88 |
+
token_value = None
|
| 89 |
+
if hf_token and getattr(hf_token, "token", None):
|
| 90 |
+
token_value = hf_token.token
|
| 91 |
+
elif os.environ.get("HF_TOKEN"):
|
| 92 |
+
token_value = os.environ.get("HF_TOKEN")
|
| 93 |
+
|
| 94 |
+
if not token_value:
|
| 95 |
+
yield "⚠️ Please log in with your Hugging Face account or set HF_TOKEN in environment."
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
client = InferenceClient(token=token_value, model="openai/gpt-oss-20b")
|
| 99 |
+
|
| 100 |
+
for chunk in client.chat_completion(
|
| 101 |
+
messages,
|
| 102 |
+
max_tokens=max_tokens,
|
| 103 |
+
stream=True,
|
| 104 |
+
temperature=temperature,
|
| 105 |
+
top_p=top_p,
|
| 106 |
+
):
|
| 107 |
+
choices = chunk.choices
|
| 108 |
+
token = ""
|
| 109 |
+
if len(choices) and choices[0].delta.content:
|
| 110 |
+
token = choices[0].delta.content
|
| 111 |
+
response += token
|
| 112 |
+
yield response
|
| 113 |
+
|
| 114 |
+
# --- Chat Interface ---
|
| 115 |
+
chatbot = gr.ChatInterface(
|
| 116 |
+
fn=respond,
|
| 117 |
+
additional_inputs=[
|
| 118 |
+
gr.Textbox(
|
| 119 |
+
value="You are Gompei the Goat, WPI's mascot. Answer questions with fun goat-like personality and real WPI facts.",
|
| 120 |
+
label="System message",
|
| 121 |
+
),
|
| 122 |
+
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
|
| 123 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 124 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 125 |
+
gr.Checkbox(label="Use Local Model", value=False),
|
| 126 |
+
],
|
| 127 |
+
type="messages",
|
| 128 |
+
examples=[
|
| 129 |
+
[
|
| 130 |
+
"Where is WPI located?",
|
| 131 |
+
"You are Gompei the Goat, WPI's mascot. Answer questions with fun goat-like personality and real WPI facts.",
|
| 132 |
+
128,
|
| 133 |
+
0.7,
|
| 134 |
+
0.95,
|
| 135 |
+
False
|
| 136 |
+
],
|
| 137 |
+
[
|
| 138 |
+
"Who founded WPI?",
|
| 139 |
+
"You are Gompei the Goat, WPI's mascot. Answer questions with fun goat-like personality and real WPI facts.",
|
| 140 |
+
128,
|
| 141 |
+
0.7,
|
| 142 |
+
0.95,
|
| 143 |
+
False
|
| 144 |
+
],
|
| 145 |
+
],
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# --- Blocks layout ---
|
| 149 |
+
with gr.Blocks(css=fancy_css) as demo:
|
| 150 |
+
with gr.Row():
|
| 151 |
+
gr.Markdown("<h1 id='title'>🐐 Chat with Gompei</h1>")
|
| 152 |
+
gr.LoginButton()
|
| 153 |
+
chatbot.render()
|
| 154 |
+
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|