Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,76 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoTokenizer, BitsAndBytesConfig, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
import spaces
|
5 |
|
6 |
+
MODEL_PATH = "benhaotang/phi4-qwq-sky-t1"
|
7 |
+
MODEL_URL = f"https://huggingface.co/{MODEL_PATH}"
|
|
|
|
|
8 |
|
9 |
+
def load_model():
|
10 |
+
bnb_config = BitsAndBytesConfig(
|
11 |
+
load_in_8bit=False,
|
12 |
+
llm_int8_enable_fp32_cpu_offload=True
|
13 |
+
)
|
14 |
+
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
MODEL_PATH,
|
17 |
+
device_map="auto",
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
offload_folder="offload_folder",
|
20 |
+
quantization_config=bnb_config
|
21 |
+
)
|
22 |
+
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
24 |
+
|
25 |
+
# Create pipeline
|
26 |
+
pipe = pipeline(
|
27 |
+
"text-generation",
|
28 |
+
model=model,
|
29 |
+
tokenizer=tokenizer,
|
30 |
+
device_map="auto",
|
31 |
+
)
|
32 |
+
|
33 |
+
return pipe
|
34 |
|
35 |
+
pipe = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
@spaces.GPU(duration=110)
|
38 |
+
def generate_response(prompt, max_length=1024):
|
39 |
+
# Convert prompt into messages format
|
40 |
+
messages = [
|
41 |
+
{"role": "system", "content": "You are a helpful AI asistent. You always think step by step."},
|
42 |
+
{"role": "user", "content": prompt}
|
43 |
+
]
|
44 |
+
|
45 |
+
# Generate response using pipeline
|
46 |
+
outputs = pipe(messages, max_new_tokens=max_length)
|
47 |
+
|
48 |
+
# Extract the generated text
|
49 |
+
response = outputs[0]["generated_text"]
|
50 |
+
|
51 |
+
# Since pipeline returns the full conversation, we want to extract just the response
|
52 |
+
# Split by the prompt and take the last part
|
53 |
+
response_only = response.split(prompt)[-1].strip()
|
54 |
+
|
55 |
+
return response_only
|
56 |
|
57 |
+
demo = gr.Interface(
|
58 |
+
fn=generate_response,
|
59 |
+
inputs=[
|
60 |
+
gr.Textbox(
|
61 |
+
label="Enter your question",
|
62 |
+
placeholder="Ask me anything...",
|
63 |
+
lines=5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
),
|
65 |
],
|
66 |
+
outputs=gr.Textbox(label="Response", lines=10),
|
67 |
+
title="benhaotang/phi4-qwq-sky-t1",
|
68 |
+
description=f""" To achieve CoT and science reasoning on small scale
|
69 |
+
|
70 |
+
Model: [benhaotang/phi4-qwq-sky-t1]({MODEL_URL})""",
|
71 |
+
examples=[
|
72 |
+
["For a scalar field theory with interaction Lagrangian $\mathcal{L}_{int} = g\phi^3 + \lambda\phi^4$:\n 1.Enumerate all possible 1-loop Feynman diagrams contributing to a 2-to-2 scattering process\n2.For each diagram, write down its corresponding amplitude\n3. Provide Mathematica code to calculate these loop amplitudes\n Please explain your reasoning step by step."]
|
73 |
+
]
|
74 |
)
|
75 |
|
76 |
+
demo.launch()
|
|
|
|