AI_Course_Generator / gradio_ui.py
aankitroy's picture
Upload folder using huggingface_hub
833a585 verified
import gradio as gr
import requests
import time # For simulating a loading effect
# API Endpoints
LOGIN_URL = "http://127.0.0.1:8000/auth/v1/login"
COURSE_API_URL = "http://127.0.0.1:8000/ai_course/v1/generate-course/"
# Hardcoded user credentials (Ideally, this should be entered dynamically)
USERNAME = "aankitroy1990@gmail.com"
PASSWORD = "xyzpassword"
# Function to authenticate and get access token
def get_access_token():
try:
response = requests.post(
LOGIN_URL,
data={
"grant_type": "password",
"username": USERNAME,
"password": PASSWORD,
"scope": "",
"client_id": "string",
"client_secret": "string",
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
response_data = response.json()
if "access_token" in response_data:
return response_data["access_token"]
else:
return None
except Exception as e:
return print(f"Failed to get access token: {str(e)}")
# Function to fetch course details with loading effect
def get_course(course_name, goal, course_type, proficiency, difficulty):
yield "**Generating course... Please wait.** ⏳" # Show loading message
access_token = get_access_token()
if not access_token:
yield "**❌ Authentication failed. Please check credentials.**"
return
headers = {"Authorization": f"Bearer {access_token}"}
payload = {
"course_name": course_name,
"goal": goal,
"course_type": course_type,
"proficiency": proficiency,
"difficulty": difficulty,
}
try:
response = requests.post(COURSE_API_URL, json=payload, headers=headers)
response_data = response.json()
if "course_content" in response_data:
yield response_data["course_content"] # Final response
else:
yield "**⚠️ Error: Unexpected response from server.**"
except Exception as e:
yield f"**❌ Failed to parse response: {str(e)}**"
# Gradio Interface
iface = gr.Interface(
fn=get_course,
inputs=[
gr.Textbox(label="Course Name"),
gr.Textbox(label="Goal/Target"),
gr.Radio(["Micro", "Full"], label="Course Type"),
gr.Radio(["Beginner", "Intermediate", "Advanced"], label="Proficiency"),
gr.Radio(["Easy", "Medium", "Hard"], label="Difficulty"),
],
outputs=gr.Markdown(label="Generated Course"),
title="AI Course Generator",
description="Create structured courses with AI.",
)
# Launch with sharing enabled
iface.launch(share=True)