🤖 StudyBuddy-AI
Generate 10 comprehensive questions and answers on any topic using gpt-4-o mini
import gradio as gr import openai import os from typing import List, Tuple import time # Initialize OpenAI client def initialize_openai(): """Initialize OpenAI client with API key""" api_key = os.getenv('OPENAI_API_KEY') if not api_key: raise ValueError("Please set your OPENAI_API_KEY environment variable") return openai.OpenAI(api_key=api_key) def generate_qa_pairs(topic: str, difficulty: str = "Medium") -> str: """ Generate 10 questions and answers for a given topic using GPT-4 Mini Args: topic (str): The topic to generate Q&A pairs for difficulty (str): Difficulty level (Easy, Medium, Hard) Returns: str: Formatted Q&A pairs """ try: client = initialize_openai() time.sleep(5) # Create the prompt prompt = f"""Generate exactly 10 questions and answers about the topic: "{topic}" Requirements: - Difficulty level: {difficulty} - Questions should be diverse and cover different aspects of the topic - Answers should be comprehensive but concise (2-4 sentences each) - Format each Q&A pair clearly with "Q:" and "A:" labels - Number each pair (1-10) Topic: {topic} Difficulty: {difficulty} Please generate the questions and answers now:""" # Make API call to GPT-4 Mini response = client.chat.completions.create( model="gpt-4o-mini", # Updated model name messages=[ { "role": "system", "content": "You are an expert educator who creates high-quality questions and answers on various topics. Your responses are well-structured, accurate, and educational." }, { "role": "user", "content": prompt } ], max_tokens=2000, temperature=0.2 ) return response.choices[0].message.content except openai.AuthenticationError: return "❌ Authentication Error: Please check your OpenAI API key." except openai.RateLimitError: return "❌ Rate Limit Error: You've exceeded your API quota. Please try again later." except openai.APIError as e: return f"❌ OpenAI API Error: {str(e)}" except Exception as e: return f"❌ Error: {str(e)}" def create_interface(): """Create and configure the Gradio interface""" with gr.Blocks( title="StudyBuddy-AI - Q&A Generator with LLM", theme=gr.themes.Soft(), css=""" .main-header { text-align: center; margin-bottom: 2rem; } .qa-output { font-family: 'Georgia', serif; line-height: 1.6; } """ ) as interface: gr.HTML("""
Generate 10 comprehensive questions and answers on any topic using gpt-4-o mini
Note: Make sure to set your OPENAI_API_KEY environment variable before running.