Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -3,7 +3,6 @@ from dotenv import load_dotenv
|
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
# Load .env variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
def summarize_dataframe(df: pd.DataFrame, max_rows: int = 30) -> str:
|
|
@@ -29,7 +28,6 @@ def query_agent(df: pd.DataFrame, query: str) -> str:
|
|
| 29 |
print("Direct analysis failed:", e)
|
| 30 |
|
| 31 |
data_text = summarize_dataframe(df)
|
| 32 |
-
|
| 33 |
prompt = f"""
|
| 34 |
You are a data analysis assistant with expertise in statistics and data interpretation.
|
| 35 |
Analyze the dataset sample below and answer the user's question in a clear, detailed, and well-explained way.
|
|
@@ -44,11 +42,13 @@ Question:
|
|
| 44 |
Answer (with explanation):
|
| 45 |
"""
|
| 46 |
|
| 47 |
-
#
|
| 48 |
client = InferenceClient(token=os.environ.get("HUGGINGFACE_API_KEY"))
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
# Extract the
|
| 52 |
answer = response[0]["generated_text"] if isinstance(response, list) and "generated_text" in response[0] else str(response)
|
|
|
|
| 53 |
|
| 54 |
return answer
|
|
|
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
def summarize_dataframe(df: pd.DataFrame, max_rows: int = 30) -> str:
|
|
|
|
| 28 |
print("Direct analysis failed:", e)
|
| 29 |
|
| 30 |
data_text = summarize_dataframe(df)
|
|
|
|
| 31 |
prompt = f"""
|
| 32 |
You are a data analysis assistant with expertise in statistics and data interpretation.
|
| 33 |
Analyze the dataset sample below and answer the user's question in a clear, detailed, and well-explained way.
|
|
|
|
| 42 |
Answer (with explanation):
|
| 43 |
"""
|
| 44 |
|
| 45 |
+
# ----------------- Option 2 starts here -----------------
|
| 46 |
client = InferenceClient(token=os.environ.get("HUGGINGFACE_API_KEY"))
|
| 47 |
+
# Use a model that supports text-generation (TGI-compatible)
|
| 48 |
+
response = client.text_generation(prompt, model="mistralai/Mistral-7B-Instruct-v0.3")
|
| 49 |
|
| 50 |
+
# Extract the text
|
| 51 |
answer = response[0]["generated_text"] if isinstance(response, list) and "generated_text" in response[0] else str(response)
|
| 52 |
+
# ----------------- Option 2 ends here -----------------
|
| 53 |
|
| 54 |
return answer
|