Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,6 +72,59 @@ def explain_sql_output(sql_query, query_result):
|
|
| 72 |
)
|
| 73 |
return chat_completion.choices[0].message.content.strip()
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
### --- Gradio Interface --- ###
|
| 77 |
tab1 = gr.Interface(
|
|
@@ -93,6 +146,14 @@ tab2 = gr.Interface(
|
|
| 93 |
description="Input a SQL query and its result. Get an AI-generated explanation."
|
| 94 |
)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
demo = gr.TabbedInterface([tab1, tab2], ["SQL Generator", "Explain Output"])
|
| 97 |
|
| 98 |
if __name__ == '__main__':
|
|
|
|
| 72 |
)
|
| 73 |
return chat_completion.choices[0].message.content.strip()
|
| 74 |
|
| 75 |
+
### --- TAB 3: Data Summary & Insights --- ###
|
| 76 |
+
|
| 77 |
+
def summarize_data(data_file):
|
| 78 |
+
try:
|
| 79 |
+
df = pd.read_csv(data_file.name)
|
| 80 |
+
except Exception:
|
| 81 |
+
try:
|
| 82 |
+
df = pd.read_json(data_file.name)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"Failed to read file: {str(e)}"
|
| 85 |
+
|
| 86 |
+
metadata = "\n".join([f"- {col}: {str(dtype)}" for col, dtype in df.dtypes.items()])
|
| 87 |
+
preview = df.head(30).to_csv(index=False)
|
| 88 |
+
|
| 89 |
+
system_prompt = """
|
| 90 |
+
You are a professional data analyst AI that specializes in summarizing datasets and uncovering insights.
|
| 91 |
+
|
| 92 |
+
Your task is to:
|
| 93 |
+
1. Provide a high-level summary of the dataset.
|
| 94 |
+
2. Identify 5 insightful observations or trends.
|
| 95 |
+
3. Highlight any interesting patterns, anomalies, or correlations you find.
|
| 96 |
+
4. Use simple, clear language understandable to non-technical users.
|
| 97 |
+
5. If relevant, suggest what kind of decisions or actions could be made based on the data.
|
| 98 |
+
|
| 99 |
+
Only use the provided preview and schema — do not assume missing data or guess columns.
|
| 100 |
+
Be helpful, concise, and specific.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
user_prompt = f"""
|
| 104 |
+
Here is a preview of the dataset (first 30 rows):
|
| 105 |
+
|
| 106 |
+
{preview}
|
| 107 |
+
|
| 108 |
+
Here is the schema (column name: type):
|
| 109 |
+
|
| 110 |
+
{metadata}
|
| 111 |
+
|
| 112 |
+
Please generate:
|
| 113 |
+
- A brief overview of the dataset.
|
| 114 |
+
- Five unique, data-driven insights or summaries.
|
| 115 |
+
- Optional: Patterns or anomalies worth noting.
|
| 116 |
+
- Keep it understandable and actionable.
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
chat_completion = client.chat.completions.create(
|
| 120 |
+
messages=[
|
| 121 |
+
{"role": "system", "content": system_prompt.strip()},
|
| 122 |
+
{"role": "user", "content": user_prompt.strip()}
|
| 123 |
+
],
|
| 124 |
+
model="llama3-70b-8192"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
return chat_completion.choices[0].message.content.strip()
|
| 128 |
|
| 129 |
### --- Gradio Interface --- ###
|
| 130 |
tab1 = gr.Interface(
|
|
|
|
| 146 |
description="Input a SQL query and its result. Get an AI-generated explanation."
|
| 147 |
)
|
| 148 |
|
| 149 |
+
tab3 = gr.Interface(
|
| 150 |
+
fn=summarize_data,
|
| 151 |
+
inputs=gr.File(label="Upload CSV or JSON Dataset"),
|
| 152 |
+
outputs="text",
|
| 153 |
+
title="Data Summary & Insights (Groq + LLaMA3)",
|
| 154 |
+
description="Upload a dataset to get a general summary and 5 AI-generated insights."
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
demo = gr.TabbedInterface([tab1, tab2], ["SQL Generator", "Explain Output"])
|
| 158 |
|
| 159 |
if __name__ == '__main__':
|