Spaces:
Running
on
Zero
Running
on
Zero
Dacho688
commited on
Commit
β’
d6042ff
1
Parent(s):
0f55eee
new app
Browse filesgit commit
- app.py +29 -23
- app_original.py +133 -0
- requirements.txt +6 -2
- test_app.py +29 -23
app.py
CHANGED
@@ -9,32 +9,37 @@ from streaming import stream_to_gradio
|
|
9 |
from huggingface_hub import login
|
10 |
from gradio.data_classes import FileData
|
11 |
|
12 |
-
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
13 |
|
14 |
llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-70B-Instruct")
|
15 |
|
16 |
agent = ReactCodeAgent(
|
17 |
tools=[],
|
18 |
llm_engine=llm_engine,
|
19 |
-
additional_authorized_imports=["numpy", "pandas", "matplotlib
|
20 |
max_iterations=10,
|
21 |
)
|
22 |
|
23 |
base_prompt = """You are an expert data analyst.
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
30 |
After each number derive real worlds insights, for instance: "Correlation between is_december and boredness is 1.3453, which suggest people are more bored in winter".
|
31 |
-
Your final answer should be a long string with at least 3 numbered and detailed parts.
|
32 |
|
33 |
Structure of the data:
|
34 |
{structure_notes}
|
35 |
|
36 |
-
|
37 |
-
DO NOT try to load data_file, it is already a dataframe pre-loaded in your python interpreter!
|
38 |
"""
|
39 |
|
40 |
example_notes="""This data is about the Titanic wreck in 1912.
|
@@ -75,9 +80,9 @@ def interact_with_agent(file_input, additional_notes):
|
|
75 |
prompt = base_prompt.format(structure_notes=data_structure_notes)
|
76 |
|
77 |
if additional_notes and len(additional_notes) > 0:
|
78 |
-
prompt +=
|
79 |
|
80 |
-
messages = [gr.ChatMessage(role="user", content=
|
81 |
yield messages + [
|
82 |
gr.ChatMessage(role="assistant", content="β³ _Starting task..._")
|
83 |
]
|
@@ -101,18 +106,19 @@ def interact_with_agent(file_input, additional_notes):
|
|
101 |
|
102 |
with gr.Blocks(
|
103 |
theme=gr.themes.Soft(
|
104 |
-
primary_hue=gr.themes.colors.
|
105 |
-
secondary_hue=gr.themes.colors.
|
106 |
)
|
107 |
) as demo:
|
108 |
gr.Markdown("""# Llama-3.1 Data analyst ππ€
|
109 |
|
110 |
-
Drop a `.csv` file below
|
|
|
111 |
file_input = gr.File(label="Your file to analyze")
|
112 |
text_input = gr.Textbox(
|
113 |
-
label="
|
114 |
)
|
115 |
-
submit = gr.Button("Run
|
116 |
chatbot = gr.Chatbot(
|
117 |
label="Data Analyst Agent",
|
118 |
type="messages",
|
@@ -121,13 +127,13 @@ Drop a `.csv` file below, add notes to describe this data if needed, and **Llama
|
|
121 |
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
122 |
),
|
123 |
)
|
124 |
-
gr.Examples(
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
)
|
129 |
|
130 |
submit.click(interact_with_agent, [file_input, text_input], [chatbot])
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
-
demo.launch()
|
|
|
9 |
from huggingface_hub import login
|
10 |
from gradio.data_classes import FileData
|
11 |
|
12 |
+
#login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
13 |
|
14 |
llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-70B-Instruct")
|
15 |
|
16 |
agent = ReactCodeAgent(
|
17 |
tools=[],
|
18 |
llm_engine=llm_engine,
|
19 |
+
additional_authorized_imports=["numpy", "pandas", "matplotlib", "seaborn","scipy"],
|
20 |
max_iterations=10,
|
21 |
)
|
22 |
|
23 |
base_prompt = """You are an expert data analyst.
|
24 |
+
You are given a data file and the data structure below.
|
25 |
+
The data file is passed to you as the variable data_file, it is a pandas dataframe, you can use it directly.
|
26 |
+
DO NOT try to load data_file, it is already a dataframe pre-loaded in your python interpreter!
|
27 |
+
|
28 |
+
When importing packages use this format: from package import module
|
29 |
+
For example: from matplotlib import pyplot as plt
|
30 |
+
Not: import matplotlib.pyplot as plt
|
31 |
+
|
32 |
+
As you work, check for NoneType values and convert to NAN.
|
33 |
|
34 |
+
Use the data file to answer the question or solve a problem given below.
|
35 |
+
|
36 |
+
In your final answer: summarize your findings
|
37 |
After each number derive real worlds insights, for instance: "Correlation between is_december and boredness is 1.3453, which suggest people are more bored in winter".
|
|
|
38 |
|
39 |
Structure of the data:
|
40 |
{structure_notes}
|
41 |
|
42 |
+
Question/Problem:
|
|
|
43 |
"""
|
44 |
|
45 |
example_notes="""This data is about the Titanic wreck in 1912.
|
|
|
80 |
prompt = base_prompt.format(structure_notes=data_structure_notes)
|
81 |
|
82 |
if additional_notes and len(additional_notes) > 0:
|
83 |
+
prompt += additional_notes
|
84 |
|
85 |
+
messages = [gr.ChatMessage(role="user", content=additional_notes)]
|
86 |
yield messages + [
|
87 |
gr.ChatMessage(role="assistant", content="β³ _Starting task..._")
|
88 |
]
|
|
|
106 |
|
107 |
with gr.Blocks(
|
108 |
theme=gr.themes.Soft(
|
109 |
+
primary_hue=gr.themes.colors.blue,
|
110 |
+
secondary_hue=gr.themes.colors.yellow,
|
111 |
)
|
112 |
) as demo:
|
113 |
gr.Markdown("""# Llama-3.1 Data analyst ππ€
|
114 |
|
115 |
+
Drop a `.csv` file below and ask a question about your data.
|
116 |
+
**Llama-3.1-70B will analyze and answer.**""")
|
117 |
file_input = gr.File(label="Your file to analyze")
|
118 |
text_input = gr.Textbox(
|
119 |
+
label="Ask a question about your data?"
|
120 |
)
|
121 |
+
submit = gr.Button("Run", variant="primary")
|
122 |
chatbot = gr.Chatbot(
|
123 |
label="Data Analyst Agent",
|
124 |
type="messages",
|
|
|
127 |
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
128 |
),
|
129 |
)
|
130 |
+
# gr.Examples(
|
131 |
+
# examples=[["./example/titanic.csv", example_notes]],
|
132 |
+
# inputs=[file_input, text_input],
|
133 |
+
# cache_examples=False
|
134 |
+
# )
|
135 |
|
136 |
submit.click(interact_with_agent, [file_input, text_input], [chatbot])
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
+
demo.launch(server_port=7861)
|
app_original.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import ReactCodeAgent, HfEngine, Tool
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
from gradio import Chatbot
|
8 |
+
from streaming import stream_to_gradio
|
9 |
+
from huggingface_hub import login
|
10 |
+
from gradio.data_classes import FileData
|
11 |
+
|
12 |
+
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
13 |
+
|
14 |
+
llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-70B-Instruct")
|
15 |
+
|
16 |
+
agent = ReactCodeAgent(
|
17 |
+
tools=[],
|
18 |
+
llm_engine=llm_engine,
|
19 |
+
additional_authorized_imports=["numpy", "pandas", "matplotlib.pyplot", "seaborn", "scipy.stats"],
|
20 |
+
max_iterations=10,
|
21 |
+
)
|
22 |
+
|
23 |
+
base_prompt = """You are an expert data analyst.
|
24 |
+
According to the features you have and the data structure given below, determine which feature should be the target.
|
25 |
+
Then list 3 interesting questions that could be asked on this data, for instance about specific correlations with target variable.
|
26 |
+
Then answer these questions one by one, by finding the relevant numbers.
|
27 |
+
Meanwhile, plot some figures using matplotlib/seaborn and save them to the (already existing) folder './figures/': take care to clear each figure with plt.clf() before doing another plot.
|
28 |
+
|
29 |
+
In your final answer: summarize these correlations and trends
|
30 |
+
After each number derive real worlds insights, for instance: "Correlation between is_december and boredness is 1.3453, which suggest people are more bored in winter".
|
31 |
+
Your final answer should be a long string with at least 3 numbered and detailed parts.
|
32 |
+
|
33 |
+
Structure of the data:
|
34 |
+
{structure_notes}
|
35 |
+
|
36 |
+
The data file is passed to you as the variable data_file, it is a pandas dataframe, you can use it directly.
|
37 |
+
DO NOT try to load data_file, it is already a dataframe pre-loaded in your python interpreter!
|
38 |
+
"""
|
39 |
+
|
40 |
+
example_notes="""This data is about the Titanic wreck in 1912.
|
41 |
+
The target figure is the survival of passengers, notes by 'Survived'
|
42 |
+
pclass: A proxy for socio-economic status (SES)
|
43 |
+
1st = Upper
|
44 |
+
2nd = Middle
|
45 |
+
3rd = Lower
|
46 |
+
age: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5
|
47 |
+
sibsp: The dataset defines family relations in this way...
|
48 |
+
Sibling = brother, sister, stepbrother, stepsister
|
49 |
+
Spouse = husband, wife (mistresses and fiancΓ©s were ignored)
|
50 |
+
parch: The dataset defines family relations in this way...
|
51 |
+
Parent = mother, father
|
52 |
+
Child = daughter, son, stepdaughter, stepson
|
53 |
+
Some children travelled only with a nanny, therefore parch=0 for them."""
|
54 |
+
|
55 |
+
def get_images_in_directory(directory):
|
56 |
+
image_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff'}
|
57 |
+
|
58 |
+
image_files = []
|
59 |
+
for root, dirs, files in os.walk(directory):
|
60 |
+
for file in files:
|
61 |
+
if os.path.splitext(file)[1].lower() in image_extensions:
|
62 |
+
image_files.append(os.path.join(root, file))
|
63 |
+
return image_files
|
64 |
+
|
65 |
+
def interact_with_agent(file_input, additional_notes):
|
66 |
+
shutil.rmtree("./figures")
|
67 |
+
os.makedirs("./figures")
|
68 |
+
|
69 |
+
data_file = pd.read_csv(file_input)
|
70 |
+
data_structure_notes = f"""- Description (output of .describe()):
|
71 |
+
{data_file.describe()}
|
72 |
+
- Columns with dtypes:
|
73 |
+
{data_file.dtypes}"""
|
74 |
+
|
75 |
+
prompt = base_prompt.format(structure_notes=data_structure_notes)
|
76 |
+
|
77 |
+
if additional_notes and len(additional_notes) > 0:
|
78 |
+
prompt += "\nAdditional notes on the data:\n" + additional_notes
|
79 |
+
|
80 |
+
messages = [gr.ChatMessage(role="user", content=prompt)]
|
81 |
+
yield messages + [
|
82 |
+
gr.ChatMessage(role="assistant", content="β³ _Starting task..._")
|
83 |
+
]
|
84 |
+
|
85 |
+
plot_image_paths = {}
|
86 |
+
for msg in stream_to_gradio(agent, prompt, data_file=data_file):
|
87 |
+
messages.append(msg)
|
88 |
+
for image_path in get_images_in_directory("./figures"):
|
89 |
+
if image_path not in plot_image_paths:
|
90 |
+
image_message = gr.ChatMessage(
|
91 |
+
role="assistant",
|
92 |
+
content=FileData(path=image_path, mime_type="image/png"),
|
93 |
+
)
|
94 |
+
plot_image_paths[image_path] = True
|
95 |
+
messages.append(image_message)
|
96 |
+
yield messages + [
|
97 |
+
gr.ChatMessage(role="assistant", content="β³ _Still processing..._")
|
98 |
+
]
|
99 |
+
yield messages
|
100 |
+
|
101 |
+
|
102 |
+
with gr.Blocks(
|
103 |
+
theme=gr.themes.Soft(
|
104 |
+
primary_hue=gr.themes.colors.yellow,
|
105 |
+
secondary_hue=gr.themes.colors.blue,
|
106 |
+
)
|
107 |
+
) as demo:
|
108 |
+
gr.Markdown("""# Llama-3.1 Data analyst ππ€
|
109 |
+
|
110 |
+
Drop a `.csv` file below, add notes to describe this data if needed, and **Llama-3.1-70B will analyze the file content and draw figures for you!**""")
|
111 |
+
file_input = gr.File(label="Your file to analyze")
|
112 |
+
text_input = gr.Textbox(
|
113 |
+
label="Additional notes to support the analysis"
|
114 |
+
)
|
115 |
+
submit = gr.Button("Run analysis!", variant="primary")
|
116 |
+
chatbot = gr.Chatbot(
|
117 |
+
label="Data Analyst Agent",
|
118 |
+
type="messages",
|
119 |
+
avatar_images=(
|
120 |
+
None,
|
121 |
+
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
122 |
+
),
|
123 |
+
)
|
124 |
+
gr.Examples(
|
125 |
+
examples=[["./example/titanic.csv", example_notes]],
|
126 |
+
inputs=[file_input, text_input],
|
127 |
+
cache_examples=False
|
128 |
+
)
|
129 |
+
|
130 |
+
submit.click(interact_with_agent, [file_input, text_input], [chatbot])
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,9 @@
|
|
1 |
-
git+https://github.com/huggingface/transformers.git#egg=transformers[agents]
|
2 |
matplotlib
|
3 |
seaborn
|
4 |
scikit-learn
|
5 |
-
scipy
|
|
|
|
|
|
|
|
|
|
1 |
+
#git+https://github.com/huggingface/transformers.git#egg=transformers[agents]
|
2 |
matplotlib
|
3 |
seaborn
|
4 |
scikit-learn
|
5 |
+
scipy
|
6 |
+
transformers
|
7 |
+
pandas==2.2.2
|
8 |
+
huggingface_hub
|
9 |
+
transformers
|
test_app.py
CHANGED
@@ -9,32 +9,37 @@ from test_streaming import stream_to_gradio
|
|
9 |
from huggingface_hub import login
|
10 |
from gradio.data_classes import FileData
|
11 |
|
12 |
-
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
13 |
|
14 |
llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-70B-Instruct")
|
15 |
|
16 |
agent = ReactCodeAgent(
|
17 |
tools=[],
|
18 |
llm_engine=llm_engine,
|
19 |
-
additional_authorized_imports=["numpy", "pandas", "matplotlib
|
20 |
max_iterations=10,
|
21 |
)
|
22 |
|
23 |
base_prompt = """You are an expert data analyst.
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
|
|
|
|
30 |
After each number derive real worlds insights, for instance: "Correlation between is_december and boredness is 1.3453, which suggest people are more bored in winter".
|
31 |
-
Your final answer should be a long string with at least 3 numbered and detailed parts.
|
32 |
|
33 |
Structure of the data:
|
34 |
{structure_notes}
|
35 |
|
36 |
-
|
37 |
-
DO NOT try to load data_file, it is already a dataframe pre-loaded in your python interpreter!
|
38 |
"""
|
39 |
|
40 |
example_notes="""This data is about the Titanic wreck in 1912.
|
@@ -75,9 +80,9 @@ def interact_with_agent(file_input, additional_notes):
|
|
75 |
prompt = base_prompt.format(structure_notes=data_structure_notes)
|
76 |
|
77 |
if additional_notes and len(additional_notes) > 0:
|
78 |
-
prompt +=
|
79 |
|
80 |
-
messages = [gr.ChatMessage(role="user", content=
|
81 |
yield messages + [
|
82 |
gr.ChatMessage(role="assistant", content="β³ _Starting task..._")
|
83 |
]
|
@@ -101,18 +106,19 @@ def interact_with_agent(file_input, additional_notes):
|
|
101 |
|
102 |
with gr.Blocks(
|
103 |
theme=gr.themes.Soft(
|
104 |
-
primary_hue=gr.themes.colors.
|
105 |
-
secondary_hue=gr.themes.colors.
|
106 |
)
|
107 |
) as demo:
|
108 |
gr.Markdown("""# Llama-3.1 Data analyst ππ€
|
109 |
|
110 |
-
Drop a `.csv` file below
|
|
|
111 |
file_input = gr.File(label="Your file to analyze")
|
112 |
text_input = gr.Textbox(
|
113 |
-
label="
|
114 |
)
|
115 |
-
submit = gr.Button("Run
|
116 |
chatbot = gr.Chatbot(
|
117 |
label="Data Analyst Agent",
|
118 |
type="messages",
|
@@ -121,13 +127,13 @@ Drop a `.csv` file below, add notes to describe this data if needed, and **Llama
|
|
121 |
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
122 |
),
|
123 |
)
|
124 |
-
gr.Examples(
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
)
|
129 |
|
130 |
submit.click(interact_with_agent, [file_input, text_input], [chatbot])
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
-
demo.launch()
|
|
|
9 |
from huggingface_hub import login
|
10 |
from gradio.data_classes import FileData
|
11 |
|
12 |
+
#login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
13 |
|
14 |
llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-70B-Instruct")
|
15 |
|
16 |
agent = ReactCodeAgent(
|
17 |
tools=[],
|
18 |
llm_engine=llm_engine,
|
19 |
+
additional_authorized_imports=["numpy", "pandas", "matplotlib", "seaborn","scipy"],
|
20 |
max_iterations=10,
|
21 |
)
|
22 |
|
23 |
base_prompt = """You are an expert data analyst.
|
24 |
+
You are given a data file and the data structure below.
|
25 |
+
The data file is passed to you as the variable data_file, it is a pandas dataframe, you can use it directly.
|
26 |
+
DO NOT try to load data_file, it is already a dataframe pre-loaded in your python interpreter!
|
27 |
+
|
28 |
+
When importing packages use this format: from package import module
|
29 |
+
For example: from matplotlib import pyplot as plt
|
30 |
+
Not: import matplotlib.pyplot as plt
|
31 |
+
|
32 |
+
As you work, check for NoneType values and convert to NAN.
|
33 |
|
34 |
+
Use the data file to answer the question or solve a problem given below.
|
35 |
+
|
36 |
+
In your final answer: summarize your findings
|
37 |
After each number derive real worlds insights, for instance: "Correlation between is_december and boredness is 1.3453, which suggest people are more bored in winter".
|
|
|
38 |
|
39 |
Structure of the data:
|
40 |
{structure_notes}
|
41 |
|
42 |
+
Question/Problem:
|
|
|
43 |
"""
|
44 |
|
45 |
example_notes="""This data is about the Titanic wreck in 1912.
|
|
|
80 |
prompt = base_prompt.format(structure_notes=data_structure_notes)
|
81 |
|
82 |
if additional_notes and len(additional_notes) > 0:
|
83 |
+
prompt += additional_notes
|
84 |
|
85 |
+
messages = [gr.ChatMessage(role="user", content=additional_notes)]
|
86 |
yield messages + [
|
87 |
gr.ChatMessage(role="assistant", content="β³ _Starting task..._")
|
88 |
]
|
|
|
106 |
|
107 |
with gr.Blocks(
|
108 |
theme=gr.themes.Soft(
|
109 |
+
primary_hue=gr.themes.colors.blue,
|
110 |
+
secondary_hue=gr.themes.colors.yellow,
|
111 |
)
|
112 |
) as demo:
|
113 |
gr.Markdown("""# Llama-3.1 Data analyst ππ€
|
114 |
|
115 |
+
Drop a `.csv` file below and ask a question about your data.
|
116 |
+
**Llama-3.1-70B will analyze and answer.**""")
|
117 |
file_input = gr.File(label="Your file to analyze")
|
118 |
text_input = gr.Textbox(
|
119 |
+
label="Ask a question about your data?"
|
120 |
)
|
121 |
+
submit = gr.Button("Run", variant="primary")
|
122 |
chatbot = gr.Chatbot(
|
123 |
label="Data Analyst Agent",
|
124 |
type="messages",
|
|
|
127 |
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
128 |
),
|
129 |
)
|
130 |
+
# gr.Examples(
|
131 |
+
# examples=[["./example/titanic.csv", example_notes]],
|
132 |
+
# inputs=[file_input, text_input],
|
133 |
+
# cache_examples=False
|
134 |
+
# )
|
135 |
|
136 |
submit.click(interact_with_agent, [file_input, text_input], [chatbot])
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
+
demo.launch(server_port=7861)
|