File size: 2,163 Bytes
26fd6a5
 
 
 
 
 
9122576
26fd6a5
 
 
 
 
 
 
 
 
 
 
 
90deb69
 
26fd6a5
9122576
 
 
26fd6a5
7354ac3
f5310c4
3621609
f5310c4
d3100ed
7354ac3
 
 
 
 
 
 
 
f5310c4
5c612ca
f5310c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3221c51
7354ac3
 
 
f5310c4
 
7354ac3
f5310c4
 
 
9122576
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
import requests
import gradio as gr
from dotenv import load_dotenv
import os
from openai import OpenAI
import spacy
import random

# Load environment variables from .env file
load_dotenv()

# Access the env
HF_TOKEN = os.getenv('HUGGING_FACE_TOKEN')

# openai setup
client = OpenAI(
  api_key=os.getenv('OPENAI_API_KEY')
)

# Global variable to control debug printing
DEBUG_MODE = True

def debug_print(*args, **kwargs):
    if DEBUG_MODE:
        print(*args, **kwargs)

def generate_questions(input_topic, num_questions, difficulty_level):
    """
    Generates EFL questions based on the input topic
    """

    # Adjusting the prompt based on the chosen difficulty level
    difficulty_instruction = {
        "Easy": "using very simple English",
        "Medium": "using moderately simple English",
        "Hard": "that challenge comprehension and critical thinking"
    }

    prompt=f"Generate {num_questions} questions {difficulty_instruction[difficulty_level]} for an EFL (English as a Foreign Language) student based on the following topic: {input_topic}"

    response = client.chat.completions.create(

        messages=[
            {"role": "system", "content": ""},
            {"role": "user", "content": prompt}
        ],

        model="gpt-3.5-turbo",
      )
    questions = response.choices[0].message.content
    debug_print("GPT output:", questions)

    return questions

with gr.Blocks() as app:
    with gr.Column():
        gr.Markdown("### EFL Question Generator")
        input_text = gr.Textbox(label="Topic", lines=1, placeholder="Type or paste your topic here...", value="Hobbies")
        with gr.Row():
            num_questions_dropdown = gr.Dropdown(label="Number of Questions", choices=[5, 10, 15, 20], value=5)
            difficulty_dropdown = gr.Dropdown(label="Difficulty Level", choices=["Easy", "Medium", "Hard"], value="Easy")
        output_questions = gr.Textbox(label="Generated Questions", lines=5)
        generate_btn = gr.Button("Generate Questions")
        generate_btn.click(fn=generate_questions, inputs=[input_text, num_questions_dropdown, difficulty_dropdown], outputs=output_questions)

app.launch()