read the PowerPoint slides and generate questions and answers based on the slides and save them into a CSV

22 Views Asked by At

it runs the script fine but once I get to running the open ai I have an issue it is giving me errors and want me to install a pip file how do I do that and how can I get this to work

the logic of this is to bring in a PowerPoint read the PowerPoint slides and generate questions and answers based on the slides and save them into a CSV file with question on the A column and answers on the B column to export so I can upload into anki for studying

import os
import csv
from pptx import Presentation
from openai_chat_gpt import GPT, Example

# Set up your OpenAI API key
openai_api_key = "your_openai_api_key"

def save_notes_to_csv(questions_answers):
    with open("notes.csv", "w", newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(["Question", "Answer"])
        writer.writerows(questions_answers)

def read_pptx_file(file_path):
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"File not found at path: {file_path}")

    presentation = Presentation(file_path)
    slides_content = []
    for slide in presentation.slides:
        slide_content = []
        for shape in slide.shapes:
            if hasattr(shape, "text"):
                slide_content.append(shape.text)
        slides_content.append(slide_content)
    return slides_content

def generate_question_from_text(text):
    # Initialize the GPT model
    gpt = GPT(engine="text-davinci-002", api_key=openai_api_key)

    # Fine-tune the model with an example
    gpt.add_example(Example("Generate a question from the text: " + text, text))

    # Generate a question
    question = gpt.get_top_reply("Generate a question from the text: " + text).text.strip()
    
    return question

def main():
    print("Welcome to Note Taking ChatGPT!")
    file_path = input("Enter the path to the PowerPoint file: ")
    try:
        slides_content = read_pptx_file(file_path)
        questions_answers = []
        for slide_content in slides_content:
            answer = "\n".join(slide_content)
            question = generate_question_from_text(answer)
            questions_answers.append([question, answer])
        save_notes_to_csv(questions_answers)
        print("Q&A generated and saved successfully to 'notes.csv'!")
    except FileNotFoundError as e:
        print(e)
        print("Please enter a valid file path.")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()

I've tryed to install pip install openai-chat-gpt

0

There are 0 best solutions below