Spaces:
Runtime error
Runtime error
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() | |