Spaces:
Sleeping
Sleeping
Update chat.py
Browse files
chat.py
CHANGED
|
@@ -249,7 +249,8 @@ class ChatHandler:
|
|
| 249 |
|
| 250 |
else:
|
| 251 |
print("No table data found in the response.")
|
| 252 |
-
|
|
|
|
| 253 |
else:
|
| 254 |
visual_response = None
|
| 255 |
return response, visual_response
|
|
@@ -298,4 +299,58 @@ def convert_column_types(df):
|
|
| 298 |
except ValueError:
|
| 299 |
# Leave as string if neither integer nor date
|
| 300 |
pass
|
| 301 |
-
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
else:
|
| 251 |
print("No table data found in the response.")
|
| 252 |
+
global_df = unstructured_text_to_df(response)
|
| 253 |
+
visual_response = create_visualization_csv(visual_query)
|
| 254 |
else:
|
| 255 |
visual_response = None
|
| 256 |
return response, visual_response
|
|
|
|
| 299 |
except ValueError:
|
| 300 |
# Leave as string if neither integer nor date
|
| 301 |
pass
|
| 302 |
+
return df
|
| 303 |
+
|
| 304 |
+
def unstructured_text_to_df(text):
|
| 305 |
+
import openai
|
| 306 |
+
import pandas as pd
|
| 307 |
+
import os
|
| 308 |
+
import json
|
| 309 |
+
|
| 310 |
+
# Your OpenAI API key
|
| 311 |
+
openai.api_key = os.getenv("OPENAI_API_KEY", "")
|
| 312 |
+
|
| 313 |
+
# Unstructured text
|
| 314 |
+
text = """The top 3 predicted materials in 2025 are:
|
| 315 |
+
1. 9762_NUT_CHNNL with a total quantity of 753
|
| 316 |
+
2. 8268_KIT_TOOL with a total quantity of 738
|
| 317 |
+
3. 5960_CABLE with a total quantity of 729"""
|
| 318 |
+
|
| 319 |
+
# OpenAI prompt to structure the data
|
| 320 |
+
prompt = f"""
|
| 321 |
+
Extract the materials and their quantities from the following text and format them as a structured JSON:
|
| 322 |
+
{text}
|
| 323 |
+
"""
|
| 324 |
+
|
| 325 |
+
# Call OpenAI API
|
| 326 |
+
response = openai.chat.completions.create(
|
| 327 |
+
model="gpt-4o-mini",
|
| 328 |
+
messages=[
|
| 329 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 330 |
+
{"role": "user", "content": prompt}
|
| 331 |
+
],
|
| 332 |
+
temperature=0
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
# Extract the response content
|
| 336 |
+
response_content = response.choices[0].message.content.strip()
|
| 337 |
+
|
| 338 |
+
# Debugging: Print raw response to check its format
|
| 339 |
+
print("Raw Response:", response_content)
|
| 340 |
+
|
| 341 |
+
# Try to parse the response as JSON
|
| 342 |
+
try:
|
| 343 |
+
structured_data = json.loads(response_content) # Parse the JSON content
|
| 344 |
+
print("Parsed JSON:", structured_data)
|
| 345 |
+
except json.JSONDecodeError:
|
| 346 |
+
print("Error: Response content is not valid JSON.")
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
# Convert the structured data into a DataFrame
|
| 350 |
+
df = pd.DataFrame(structured_data["materials"])
|
| 351 |
+
|
| 352 |
+
# Rename columns to desired format
|
| 353 |
+
df.columns = ["material_name", "material_quantity"]
|
| 354 |
+
|
| 355 |
+
# Print the DataFrame
|
| 356 |
+
print(df)
|