File size: 3,176 Bytes
0faa11e
 
4e7551b
0faa11e
 
4e7551b
 
 
 
 
 
 
0faa11e
 
4e7551b
 
 
0faa11e
4e7551b
 
0faa11e
4e7551b
0faa11e
4e7551b
 
 
 
0faa11e
4e7551b
 
 
 
0faa11e
4e7551b
 
0faa11e
4e7551b
0faa11e
4e7551b
 
 
0faa11e
 
 
 
 
4e7551b
0faa11e
 
4e7551b
0faa11e
 
 
4e7551b
0faa11e
4e7551b
 
 
 
0faa11e
 
4e7551b
 
 
 
 
 
 
 
 
 
 
 
 
 
0faa11e
 
4e7551b
 
 
 
0faa11e
4e7551b
 
0faa11e
4e7551b
 
0faa11e
 
4e7551b
 
 
0faa11e
4e7551b
 
 
0faa11e
 
4e7551b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()