Spaces:
Build error
Build error
Update chat.py
Browse files248 unmodified lines24925025125225325425542 unmodified lines298299300301248 unmodified lineselse:print("No table data found in the response.")visual_response = Noneelse:visual_response = Nonereturn response, visual_response42 unmodified linesexcept ValueError:# Leave as string if neither integer nor datepassreturn df248 unmodified lines24925025125225325425525642 unmodified lines299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356248 unmodified lineselse:print("No table data found in the response.")global_df = unstructured_text_to_df(response)visual_response = create_visualization_csv(visual_query)else:visual_response = Nonereturn response, visual_response42 unmodified linesexcept ValueError:# Leave as string if neither integer nor datepassreturn dfdef unstructured_text_to_df(text):import openaiimport pandas as pdimport osimport json# Your OpenAI API keyopenai.api_key = os.getenv("OPENAI_API_KEY", "")# Unstructured texttext = """The top 3 predicted materials in 2025 are:1. 9762_NUT_CHNNL with a total quantity of 7532. 8268_KIT_TOOL with a total quantity of 7383. 5960_CABLE with a total quantity of 729"""# OpenAI prompt to structure the dataprompt = f"""Extract the materials and their quantities from the following text and format them as a structured JSON:{text}"""# Call OpenAI APIresponse = openai.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": prompt}],temperature=0)# Extract the response contentresponse_content = response.choices[0].message.content.strip()# Debugging: Print raw response to check its formatprint("Raw Response:", response_content)# Try to parse the response as JSONtry:structured_data = json.loads(response_content) # Parse the JSON contentprint("Parsed JSON:", structured_data)except json.JSONDecodeError:print("Error: Response content is not valid JSON.")# Convert the structured data into a DataFramedf = pd.DataFrame(structured_data["materials"])# Rename columns to desired formatdf.columns = ["material_name", "material_quantity"]# Print the DataFrameprint(df)
chat.py+57-2