import gradio as gr import os import requests from huggingface_hub import InferenceClient # Initialize the Hugging Face Inference client for DeepSeek def get_inference_client(): api_token = os.environ.get("HUGGINGFACE_TOKEN") if not api_token: return None client = InferenceClient(token=api_token) return client def generate_math_questions(chapter, num_questions=5, difficulty="medium"): """Generate mathematics questions based on the provided chapter/topic""" client = get_inference_client() if not client: return "Error: Hugging Face token not configured. Please set the HUGGINGFACE_TOKEN in your space settings." prompt = f"""Generate {num_questions} {difficulty}-level mathematics questions about {chapter}. For each question: 1. Write a clear, well-defined question 2. Provide a step-by-step solution 3. Include the final answer Format your response as: ## Question 1 [Question text] ### Solution [Step-by-step solution] ### Answer [Final answer] ## Question 2 ...and so on """ try: response = client.text_generation( prompt=prompt, model="deepseek-ai/deepseek-math-7b-instruct", max_new_tokens=2048, temperature=0.7, top_p=0.95, ) return response except Exception as e: return f"Error generating questions: {str(e)}\n\nPlease ensure your Hugging Face token has the necessary permissions." # Create the Gradio interface with gr.Blocks(title="Mathematics Question Generator") as demo: gr.Markdown("# 🧮 Mathematics Question Generator") gr.Markdown("Enter a mathematics chapter or topic to generate practice questions with solutions.") with gr.Row(): with gr.Column(scale=3): chapter_input = gr.Textbox( label="Mathematics Topic/Chapter", placeholder="e.g., Calculus: Integration by Parts", info="Be specific for better results" ) with gr.Row(): num_questions = gr.Slider( minimum=1, maximum=10, value=5, step=1, label="Number of Questions" ) difficulty = gr.Dropdown( choices=["elementary", "easy", "medium", "hard", "advanced"], value="medium", label="Difficulty Level" ) generate_button = gr.Button("Generate Questions", variant="primary") with gr.Row(): output = gr.Markdown(label="Generated Questions") generate_button.click( fn=generate_math_questions, inputs=[chapter_input, num_questions, difficulty], outputs=output ) gr.Markdown("---") gr.Markdown("Created by Kamagelo Mosia | Powered by DeepSeek and Hugging Face") # Launch the app demo.launch()