Spaces:
Sleeping
Sleeping
Update message format and llama version
Browse files- app.py +28 -22
- figures/.DS_Store +0 -0
- figures/mascot.png +0 -0
- requirements.txt +0 -1
- streaming.py +64 -0
app.py
CHANGED
@@ -4,18 +4,14 @@ import gradio as gr
|
|
4 |
from transformers import ReactCodeAgent, HfEngine, Tool
|
5 |
import pandas as pd
|
6 |
|
7 |
-
from
|
8 |
-
|
9 |
-
stream_from_transformers_agent,
|
10 |
-
ChatMessage,
|
11 |
-
ChatFileMessage,
|
12 |
-
)
|
13 |
from huggingface_hub import login
|
14 |
from gradio.data_classes import FileData
|
15 |
|
16 |
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
17 |
|
18 |
-
llm_engine = HfEngine("meta-llama/Meta-Llama-3-70B-Instruct")
|
19 |
|
20 |
agent = ReactCodeAgent(
|
21 |
tools=[],
|
@@ -25,7 +21,6 @@ agent = ReactCodeAgent(
|
|
25 |
)
|
26 |
|
27 |
base_prompt = """You are an expert data analyst.
|
28 |
-
Please load the source file with pandas (you cannot use 'os' module).
|
29 |
According to the features you have and the dta structure given below, determine which feature should be the target.
|
30 |
Then list 3 interesting questions that could be asked on this data, for instance about specific correlations with target variable.
|
31 |
Then answer these questions one by one, by finding the relevant numbers.
|
@@ -35,9 +30,11 @@ In your final answer: summarize these correlations and trends
|
|
35 |
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".
|
36 |
Your final answer should be a long string with at least 3 numbered and detailed parts.
|
37 |
|
38 |
-
Source file for the data = {source_file}
|
39 |
Structure of the data:
|
40 |
{structure_notes}
|
|
|
|
|
|
|
41 |
"""
|
42 |
|
43 |
example_notes="""This data is about the Titanic wreck in 1912.
|
@@ -69,34 +66,36 @@ def interact_with_agent(file_input, additional_notes):
|
|
69 |
shutil.rmtree("./figures")
|
70 |
os.makedirs("./figures")
|
71 |
|
72 |
-
|
73 |
data_structure_notes = f"""- Description (output of .describe()):
|
74 |
-
{
|
75 |
- Columns with dtypes:
|
76 |
-
{
|
77 |
|
78 |
-
prompt = base_prompt.format(
|
79 |
|
80 |
if additional_notes and len(additional_notes) > 0:
|
81 |
prompt += "\nAdditional notes on the data:\n" + additional_notes
|
82 |
|
83 |
-
messages = [ChatMessage(role="user", content=prompt
|
84 |
-
yield messages
|
|
|
|
|
85 |
|
86 |
plot_image_paths = {}
|
87 |
-
for msg in
|
88 |
messages.append(msg)
|
89 |
for image_path in get_images_in_directory("./figures"):
|
90 |
if image_path not in plot_image_paths:
|
91 |
-
image_message =
|
92 |
role="assistant",
|
93 |
-
|
94 |
-
content="",
|
95 |
-
thought=True,
|
96 |
)
|
97 |
plot_image_paths[image_path] = True
|
98 |
messages.append(image_message)
|
99 |
-
yield messages
|
|
|
|
|
100 |
yield messages
|
101 |
|
102 |
|
@@ -109,7 +108,14 @@ Drop a `.csv` file below, add notes to describe this data if needed, and **Llama
|
|
109 |
label="Additional notes to support the analysis"
|
110 |
)
|
111 |
submit = gr.Button("Run analysis!")
|
112 |
-
chatbot =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
gr.Examples(
|
114 |
examples=[["./example/titanic.csv", example_notes]],
|
115 |
inputs=[file_input, text_input],
|
|
|
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=[],
|
|
|
21 |
)
|
22 |
|
23 |
base_prompt = """You are an expert data analyst.
|
|
|
24 |
According to the features you have and the dta 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.
|
|
|
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.
|
|
|
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="⏳ _Task not finished yet!_")
|
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="⏳ _Task not finished yet!_")
|
98 |
+
]
|
99 |
yield messages
|
100 |
|
101 |
|
|
|
108 |
label="Additional notes to support the analysis"
|
109 |
)
|
110 |
submit = gr.Button("Run analysis!")
|
111 |
+
chatbot = gr.Chatbot(
|
112 |
+
label="Agent",
|
113 |
+
type="messages",
|
114 |
+
avatar_images=(
|
115 |
+
None,
|
116 |
+
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
117 |
+
),
|
118 |
+
)
|
119 |
gr.Examples(
|
120 |
examples=[["./example/titanic.csv", example_notes]],
|
121 |
inputs=[file_input, text_input],
|
figures/.DS_Store
DELETED
Binary file (6.15 kB)
|
|
figures/mascot.png
DELETED
Binary file (58.1 kB)
|
|
requirements.txt
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
gradio_agentchatbot
|
2 |
git+https://github.com/huggingface/transformers.git#egg=transformers[agents]
|
3 |
matplotlib
|
4 |
seaborn
|
|
|
|
|
1 |
git+https://github.com/huggingface/transformers.git#egg=transformers[agents]
|
2 |
matplotlib
|
3 |
seaborn
|
streaming.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers.agents.agent_types import AgentAudio, AgentImage, AgentText, AgentType
|
2 |
+
from transformers.agents import ReactAgent
|
3 |
+
|
4 |
+
|
5 |
+
def pull_message(step_log: dict):
|
6 |
+
try:
|
7 |
+
from gradio import ChatMessage
|
8 |
+
except ImportError:
|
9 |
+
raise ImportError("Gradio should be installed in order to launch a gradio demo.")
|
10 |
+
|
11 |
+
if step_log.get("rationale"):
|
12 |
+
yield ChatMessage(role="assistant", content=step_log["rationale"])
|
13 |
+
if step_log.get("tool_call"):
|
14 |
+
used_code = step_log["tool_call"]["tool_name"] == "code interpreter"
|
15 |
+
content = step_log["tool_call"]["tool_arguments"]
|
16 |
+
if used_code:
|
17 |
+
content = f"```py\n{content}\n```"
|
18 |
+
yield ChatMessage(
|
19 |
+
role="assistant",
|
20 |
+
metadata={"title": f"🛠️ Used tool {step_log['tool_call']['tool_name']}"},
|
21 |
+
content=content,
|
22 |
+
)
|
23 |
+
if step_log.get("observation"):
|
24 |
+
yield ChatMessage(role="assistant", content=f"```\n{step_log['observation']}\n```")
|
25 |
+
if step_log.get("error"):
|
26 |
+
yield ChatMessage(
|
27 |
+
role="assistant",
|
28 |
+
content=str(step_log["error"]),
|
29 |
+
metadata={"title": "💥 Error"},
|
30 |
+
)
|
31 |
+
|
32 |
+
|
33 |
+
def stream_to_gradio(agent: ReactAgent, task: str, **kwargs):
|
34 |
+
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
35 |
+
|
36 |
+
try:
|
37 |
+
from gradio import ChatMessage
|
38 |
+
except ImportError:
|
39 |
+
raise ImportError("Gradio should be installed in order to launch a gradio demo.")
|
40 |
+
|
41 |
+
class Output:
|
42 |
+
output: AgentType | str = None
|
43 |
+
|
44 |
+
for step_log in agent.run(task, stream=True, **kwargs):
|
45 |
+
if isinstance(step_log, dict):
|
46 |
+
for message in pull_message(step_log):
|
47 |
+
print("message", message)
|
48 |
+
yield message
|
49 |
+
|
50 |
+
Output.output = step_log
|
51 |
+
if isinstance(Output.output, AgentText):
|
52 |
+
yield ChatMessage(role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```")
|
53 |
+
elif isinstance(Output.output, AgentImage):
|
54 |
+
yield ChatMessage(
|
55 |
+
role="assistant",
|
56 |
+
content={"path": Output.output.to_string(), "mime_type": "image/png"},
|
57 |
+
)
|
58 |
+
elif isinstance(Output.output, AgentAudio):
|
59 |
+
yield ChatMessage(
|
60 |
+
role="assistant",
|
61 |
+
content={"path": Output.output.to_string(), "mime_type": "audio/wav"},
|
62 |
+
)
|
63 |
+
else:
|
64 |
+
yield ChatMessage(role="assistant", content=Output.output)
|