xinjiboy1004 commited on
Commit
02308ef
Β·
verified Β·
1 Parent(s): 6425b58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -1
app.py CHANGED
@@ -1,7 +1,71 @@
1
  import gradio as gr
2
 
3
  def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  demo.launch()
 
1
  import gradio as gr
2
 
3
  def greet(name):
4
+ f = hello()
5
+ return f
6
+
7
+ def hello():
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
+
10
+ # Using pandas to read some structured data
11
+ import pandas as pd
12
+ from io import StringIO
13
+
14
+ # single table
15
+ EXAMPLE_CSV_CONTENT = """
16
+ "Loss","Date","Score","Opponent","Record","Attendance"
17
+ "Hampton (14–12)","September 25","8–7","Padres","67–84","31,193"
18
+ "Speier (5–3)","September 26","3–1","Padres","67–85","30,711"
19
+ "Elarton (4–9)","September 22","3–1","@ Expos","65–83","9,707"
20
+ "Lundquist (0–1)","September 24","15–11","Padres","67–83","30,774"
21
+ "Hampton (13–11)","September 6","9–5","Dodgers","61–78","31,407"
22
+ """
23
+
24
+ csv_file = StringIO(EXAMPLE_CSV_CONTENT)
25
+ df = pd.read_csv(csv_file)
26
+
27
+ model_name = "tablegpt/TableGPT2-7B"
28
+
29
+ model = AutoModelForCausalLM.from_pretrained(
30
+ model_name, torch_dtype="auto", device_map="auto"
31
+ )
32
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
33
+
34
+ example_prompt_template = """Given access to several pandas dataframes, write the Python code to answer the user's question.
35
+
36
+ /*
37
+ "{var_name}.head(5).to_string(index=False)" as follows:
38
+ {df_info}
39
+ */
40
+
41
+ Question: {user_question}
42
+ """
43
+ question = "ε“ͺδΊ›ζ―”θ΅›ηš„ζˆ˜η»©θΎΎεˆ°δΊ†40θƒœ40负?"
44
+
45
+ prompt = example_prompt_template.format(
46
+ var_name="df",
47
+ df_info=df.head(5).to_string(index=False),
48
+ user_question=question,
49
+ )
50
+
51
+ messages = [
52
+ {"role": "system", "content": "You are a helpful assistant."},
53
+ {"role": "user", "content": prompt},
54
+ ]
55
+ text = tokenizer.apply_chat_template(
56
+ messages, tokenize=False, add_generation_prompt=True
57
+ )
58
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
59
+
60
+ generated_ids = model.generate(**model_inputs, max_new_tokens=512)
61
+ generated_ids = [
62
+ output_ids[len(input_ids) :]
63
+ for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
64
+ ]
65
+
66
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
67
+ return response
68
+
69
 
70
  demo = gr.Interface(fn=greet, inputs="text", outputs="text")
71
  demo.launch()