ValueError: DataFrame constructor not properly called!

Hi!

My app link is: https://mcqs-gen.streamlit.app/

Github Repository link: GitHub - ziayounasch/genai

I have tried this app on 3.8 and 3.11 version of python and the latest streamlit version.

I am getting the following error while uploading the file and running the app:

────────────────────── Traceback (most recent call last) ───────────────────────
  /home/adminuser/venv/lib/python3.8/site-packages/streamlit/runtime/scriptrun  
  ner/script_runner.py:600 in _run_script                                       
                                                                                
  /mount/src/genai/StreamlitApp.py:71 in <module>                               
                                                                                
    68 │   │   │   │   │   if quiz is not None:                                 
    69 │   │   │   │   │   │   table_data=get_table_data(quiz)                  
    70 │   │   │   │   │   │   if table_data is not None:                       
  ❱ 71 │   │   │   │   │   │   │   df=pd.DataFrame(table_data)                  
    72 │   │   │   │   │   │   │   df.index=df.index+1                          
    73 │   │   │   │   │   │   │   st.table(df)                                 
    74 │   │   │   │   │   │   │   #Display the review in atext box as well     
                                                                                
  /home/adminuser/venv/lib/python3.8/site-packages/pandas/core/frame.py:817 in  
  __init__                                                                      
                                                                                
      814 │   │   # For data is scalar                                          
      815 │   │   else:                                                         
      816 │   │   │   if index is None or columns is None:                      
  ❱   817 │   │   │   │   raise ValueError("DataFrame constructor not properly  
      818 │   │   │                                                             
      819 │   │   │   index = ensure_index(index)                               
      820 │   │   │   columns = ensure_index(columns)                           
────────────────────────────────────────────────────────────────────────────────
ValueError: DataFrame constructor not properly called!

I will really appreciate your help in this regard.

Thanks!

Hi @Zia_Younas,

The error “DataFrame constructor not properly called!” typically means there’s an issue with the data format or structure provided to pd.DataFrame().

To resolve this, you can check that table_data is correctly structured (e.g., a dictionary of lists, a list of dictionaries, or a 2D list). Also, make sure table_data is not empty or None before passing it to the DataFrame constructor.

I hope that helps.

Best,
Charly

Thanks @Charly_Wargnier for your response, I really appreciate it.

I have checked the code several time and it seems ok… The app was fully functional 3 days back and then afterwards it started throwing this error… But now when I run the app it generates MCQs at the back end which can be seen in the manage app log but don’t show in the streamlit app and throws this error.

Can you please help me with this?

Thanks,
Zia

You need to check the data too.

I am uploading pdf file with text in it… 3 days back it was generating output with these pdfs.

As said above, you need to check what you have in table_data.

dictionary of mcqs generated by the model

That is weird, by looking at the code in your repo it looks like it should be a list. How did you check that?

Here you can observe:

def get_table_data(quiz_str):
    try:
        # convert the quiz from a str to dict
        quiz_dict=json.loads(quiz_str)
        quiz_table_data=[]
        
        # iterate over the quiz dictionary and extract the required information
        for key,value in quiz_dict.items():
            mcq=value["mcq"]
            options=" || ".join(
                [
                    f"{option}-> {option_value}" for option, option_value in value["options"].items()
                 
                 ]
            )
            
            correct=value["correct"]
            quiz_table_data.append({"MCQ": mcq,"Choices": options, "Correct": correct})
        
        return quiz_table_data
        
    except Exception as e:
        traceback.print_exception(type(e), e, e.__traceback__)
        return False

Yeah, that is why I expect table_data to be a list, not a dictionary.

Anyway, looking at the code is not enough, because the code may not be doing what you think it does. So you need to check the data too.

How can I check the data?

By displaying it or making assertions about it. What does st.text(table_data) shows right before the error happens?

@Goyo I had run the st.text(table_data) right before error happens and it returned False. The screenshot is attached.

So neither a dictionary nor a list. What do you want to happen when table_data is False?

@Goyo I want it to display the MCQs on this Streamlit app generated by it, which are getting generated in the logs but are not getting displayed on the app.

But when table_data is False there is no MCQs there to display.

But it is printing the MCQs in Logs which means MCQs are getting generated… any suggestions what should I do now? 5-6 days back the app was working fine afterwards it started throwing error.

Where in your code are you printing the MCQs to the logs?

If the MCQs are getting generated, they are not getting their way into table_data. You need to figure out why that is happening

@Goyo That is what I couldn’t figure out yet… can you please guide me how to debug this issue?

get_table_data returns False when an exception is raised. You need to look at that exception.