Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
def greet(name):
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|