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_prompt): | |
""" | |
Generates EFL questions based on the input text using OpenAI's GPT-3. | |
""" | |
prompt=f"Generate 5 simple questions for an EFL (English as a Foreign Language) based on the following topic: {input_prompt}" | |
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="China") | |
output_questions = gr.Textbox(label="Generated Questions", lines=5) | |
generate_btn = gr.Button("Generate Questions") | |
generate_btn.click(fn=generate_questions, inputs=input_text, outputs=output_questions) | |
app.launch() | |