PKU-ML commited on
Commit
d7d384c
·
verified ·
1 Parent(s): 25e65c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -11
app.py CHANGED
@@ -1,18 +1,73 @@
 
 
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
5
 
6
- def predict(input_img):
7
- predictions = pipeline(input_img)
8
- return input_img, {p["label"]: p["score"] for p in predictions}
9
 
10
- gradio_app = gr.Interface(
11
- predict,
12
- inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
13
- outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
14
- title="Hot Dog? Or Not?",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
 
17
  if __name__ == "__main__":
18
- gradio_app.launch()
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
 
4
 
 
5
 
 
 
 
6
 
7
+ # GPU
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ print(f"Using device: {device}")
10
+
11
+
12
+ model_path = "PKU-ML/G1-7B"
13
+ print("Loading model...")
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ model_path,
16
+ torch_dtype="auto",
17
+ device_map="auto"
18
+ ).to(device)
19
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
20
+
21
+
22
+ INSTRUCTION_TEMPLATE = """
23
+ {instruction}
24
+
25
+ Solve the above problem efficiently and clearly. The last line of your response should be of the following format: 'Therefore, the final answer is: $\\boxed{{ANSWER}}$. I hope it is correct' (without quotes) where ANSWER is just the final number or expression that solves the problem. Think step by step before answering.
26
+ """.strip()
27
+
28
+
29
+
30
+ def generate_response(prompt):
31
+ model.eval()
32
+
33
+ messages = [
34
+ {"role": "user", "content": INSTRUCTION_TEMPLATE.format(instruction=prompt)}
35
+ ]
36
+ text = tokenizer.apply_chat_template(
37
+ messages,
38
+ tokenize=False,
39
+ add_generation_prompt=True
40
+ )
41
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
42
+
43
+ generated_ids = model.generate(
44
+ **model_inputs,
45
+ max_new_tokens=4096,
46
+ top_p=0.95,
47
+ top_k=30,
48
+ temperature=0.6
49
+ )
50
+ generated_ids = [
51
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
52
+ ]
53
+
54
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
55
+ return response
56
+
57
+
58
+ interface = gr.Interface(
59
+ fn=generate_response,
60
+ inputs=[
61
+ gr.Textbox(label="Your Message", placeholder="Write your question..."),
62
+ # gr.Slider(label="Max Length", minimum=50, maximum=200, step=10, value=100),
63
+ # gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, step=0.05, value=0.65),
64
+ # gr.Slider(label="Top-p (nucleus)", minimum=0.1, maximum=1.0, step=0.05, value=0.8),
65
+ ],
66
+ outputs=gr.Textbox(label="Response"),
67
+ title="G1",
68
+ description="Ask a graph reasoning question",
69
+ theme="huggingface",
70
  )
71
 
72
  if __name__ == "__main__":
73
+ interface.launch()