File size: 2,692 Bytes
833a585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)