Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,133 +1,164 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
Then
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
]
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
demo.launch()
|
|
|
|
| 1 |
+
# NCTC
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import ReactCodeAgent, HfEngine, Tool
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
from gradio import Chatbot
|
| 9 |
+
from transformers.agents import stream_to_gradio
|
| 10 |
+
from huggingface_hub import login
|
| 11 |
+
from gradio.data_classes import FileData
|
| 12 |
+
|
| 13 |
+
login(os.getenv("HUGGINGFACEHUB_API_TOKEN"))
|
| 14 |
+
|
| 15 |
+
llm_engine = HfEngine("meta-llama/Meta-Llama-3.1-70B-Instruct")
|
| 16 |
+
|
| 17 |
+
agent = ReactCodeAgent(
|
| 18 |
+
tools=[],
|
| 19 |
+
llm_engine=llm_engine,
|
| 20 |
+
additional_authorized_imports=["numpy", "pandas", "matplotlib.pyplot", "seaborn", "scipy.stats"],
|
| 21 |
+
max_iterations=10,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# base_prompt = """You are an expert data analyst of National Customs Targeting Center. You will be uploaded with CSV file with multiple columns of numerical , categorical and text variables.
|
| 25 |
+
# According to the features you have and the data structure given below, determine which feature should be the target.
|
| 26 |
+
# Then list 3 interesting questions that could be asked on this data, for instance about specific correlations with target variable.
|
| 27 |
+
# Then answer these questions one by one, by finding the relevant numbers.
|
| 28 |
+
# 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.
|
| 29 |
+
|
| 30 |
+
# In your final answer: summarize these correlations and trends
|
| 31 |
+
# 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".
|
| 32 |
+
# Your final answer should be a long string with at least 3 numbered and detailed parts.
|
| 33 |
+
|
| 34 |
+
# Structure of the data:
|
| 35 |
+
# {structure_notes}
|
| 36 |
+
|
| 37 |
+
# The data file is passed to you as the variable data_file, it is a pandas dataframe, you can use it directly.
|
| 38 |
+
# DO NOT try to load data_file, it is already a dataframe pre-loaded in your python interpreter!
|
| 39 |
+
# """
|
| 40 |
+
base_prompt = """You are an expert data analyst at the National Customs Targeting Center. You will be provided with a CSV file containing multiple columns of numerical, categorical, and text variables.
|
| 41 |
+
|
| 42 |
+
Your tasks are:
|
| 43 |
+
1. **Target Identification**:
|
| 44 |
+
- Determine which feature(s) should be the target for analysis. Focus primarily on numerical and categorical columns, and avoid using unstructured text columns as targets.
|
| 45 |
+
|
| 46 |
+
2. **Generate Interesting Questions**:
|
| 47 |
+
- Based on the identified target features, list at least 3 interesting questions that could be asked. For instance, explore specific correlations with the target variable(s), trends, or patterns.
|
| 48 |
+
|
| 49 |
+
3. **Answer the Questions**:
|
| 50 |
+
- Answer these questions one by one by analyzing the data and finding relevant numbers.
|
| 51 |
+
- Generate insights from these answers. For example: "Correlation between `is_december` and `boredness` is 1.3453, suggesting that people are more bored in winter."
|
| 52 |
+
|
| 53 |
+
4. **Generate Outlier Insights**:
|
| 54 |
+
- Identify outliers for each variable in the dataset.
|
| 55 |
+
- Provide insights into the outliers, including printing the outlier records and explaining their significance.
|
| 56 |
+
|
| 57 |
+
5. **Visualization**:
|
| 58 |
+
- Plot multiple figures using matplotlib or seaborn.
|
| 59 |
+
- Generate plots for various target columns, covering both numerical and categorical columns.
|
| 60 |
+
- Ensure each figure is saved to the './figures/' folder and clear each figure with `plt.clf()` before generating the next plot.
|
| 61 |
+
- Include relevant plots that visualize correlations, trends, distributions, and outliers.
|
| 62 |
+
|
| 63 |
+
6. **Final Summary**:
|
| 64 |
+
- Summarize the correlations, trends, and outlier insights in a detailed manner. Provide at least 3 numbered and detailed parts in the summary.
|
| 65 |
+
|
| 66 |
+
Structure of the data:
|
| 67 |
+
{structure_notes}
|
| 68 |
+
|
| 69 |
+
The data file is passed to you as the variable `data_file`, which is a pandas dataframe, and you can use it directly. DO NOT try to load `data_file`, as it is already pre-loaded in your Python interpreter!
|
| 70 |
+
|
| 71 |
+
Your final output should include:
|
| 72 |
+
1. The identified target feature(s).
|
| 73 |
+
2. Three interesting questions with detailed answers and real-world insights.
|
| 74 |
+
3. Outlier insights for each variable, including the outlier records.
|
| 75 |
+
4. Multiple saved plots in the './figures/' folder.
|
| 76 |
+
5. A long, detailed final summary.
|
| 77 |
+
"""
|
| 78 |
+
example_notes="""This data is about a sample Customs dataset with products imports (IMP_DESC),Importer ID( IEC No.), SUPPLIER ID , (item unit price) ITEM_UPI , CTH for product classification (Declared CTH), declared BCD Notification benefit (BCD Notification No. Declared) and value of import (ITEM_ASSESS_VAL)"""
|
| 79 |
+
|
| 80 |
+
def get_images_in_directory(directory):
|
| 81 |
+
image_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff'}
|
| 82 |
+
|
| 83 |
+
image_files = []
|
| 84 |
+
for root, dirs, files in os.walk(directory):
|
| 85 |
+
for file in files:
|
| 86 |
+
if os.path.splitext(file)[1].lower() in image_extensions:
|
| 87 |
+
image_files.append(os.path.join(root, file))
|
| 88 |
+
return image_files
|
| 89 |
+
|
| 90 |
+
def interact_with_agent(file_input, additional_notes):
|
| 91 |
+
shutil.rmtree("./figures")
|
| 92 |
+
os.makedirs("./figures")
|
| 93 |
+
|
| 94 |
+
data_file = pd.read_csv(file_input)
|
| 95 |
+
data_structure_notes = f"""- Description (output of .describe()):
|
| 96 |
+
{data_file.describe()}
|
| 97 |
+
- Columns with dtypes:
|
| 98 |
+
{data_file.dtypes}"""
|
| 99 |
+
|
| 100 |
+
prompt = base_prompt.format(structure_notes=data_structure_notes)
|
| 101 |
+
|
| 102 |
+
if additional_notes and len(additional_notes) > 0:
|
| 103 |
+
prompt += "\nAdditional notes on the data:\n" + additional_notes
|
| 104 |
+
|
| 105 |
+
messages = [gr.ChatMessage(role="user", content=prompt)]
|
| 106 |
+
yield messages + [
|
| 107 |
+
gr.ChatMessage(role="assistant", content="⏳ _Starting task..._")
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
plot_image_paths = {}
|
| 111 |
+
for msg in stream_to_gradio(agent, prompt, data_file=data_file):
|
| 112 |
+
messages.append(msg)
|
| 113 |
+
for image_path in get_images_in_directory("./figures"):
|
| 114 |
+
if image_path not in plot_image_paths:
|
| 115 |
+
image_message = gr.ChatMessage(
|
| 116 |
+
role="assistant",
|
| 117 |
+
content=FileData(path=image_path, mime_type="image/png"),
|
| 118 |
+
)
|
| 119 |
+
plot_image_paths[image_path] = True
|
| 120 |
+
messages.append(image_message)
|
| 121 |
+
yield messages + [
|
| 122 |
+
gr.ChatMessage(role="assistant", content="⏳ _Still processing..._")
|
| 123 |
+
]
|
| 124 |
+
yield messages
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
import gradio as gr
|
| 128 |
+
|
| 129 |
+
with gr.Blocks(
|
| 130 |
+
theme=gr.themes.Soft(
|
| 131 |
+
primary_hue=gr.themes.colors.yellow,
|
| 132 |
+
secondary_hue=gr.themes.colors.blue,
|
| 133 |
+
)
|
| 134 |
+
) as demo:
|
| 135 |
+
gr.Markdown("""
|
| 136 |
+
<h1 style='color: darkblue; font-size: 2.5em;'>Llama-3.1 Data Analyst 📊🤔</h1>
|
| 137 |
+
<p><b>NCTC's attempt to use LLM-based ReAct Autonomous Agents to assist in smart customs data analysis</b></p>
|
| 138 |
+
<p>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!</p>
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
file_input = gr.File(label="Your file to analyze")
|
| 142 |
+
text_input = gr.Textbox(
|
| 143 |
+
label="Additional notes to support the analysis"
|
| 144 |
+
)
|
| 145 |
+
submit = gr.Button("Run analysis!", variant="primary")
|
| 146 |
+
chatbot = gr.Chatbot(
|
| 147 |
+
label="Data Analyst Agent",
|
| 148 |
+
type="messages",
|
| 149 |
+
avatar_images=(
|
| 150 |
+
None,
|
| 151 |
+
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
| 152 |
+
),
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
gr.Examples(
|
| 156 |
+
examples=[["./example/titanic.csv", "Example notes on Titanic dataset."]],
|
| 157 |
+
inputs=[file_input, text_input],
|
| 158 |
+
cache_examples=False
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
submit.click(interact_with_agent, [file_input, text_input], [chatbot])
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
demo.launch()
|