File size: 3,634 Bytes
41146ef
34bbf8e
 
9023ac5
34bbf8e
9023ac5
 
 
34bbf8e
 
 
 
 
9023ac5
 
 
34bbf8e
9023ac5
34bbf8e
9023ac5
34bbf8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9023ac5
34bbf8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9023ac5
 
34bbf8e
 
 
 
 
9023ac5
34bbf8e
 
 
 
9023ac5
 
 
34bbf8e
9023ac5
 
34bbf8e
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
import uuid
import logging
from src.controller.agent_cacher import AgentManager

# Configure logging
logging.basicConfig(level=logging.INFO, format="-->%(asctime)s [%(levelname)s] %(message)s")

# User credentials
VALID_USERS = {
    "ankit": "mlpass123",
    "demo": "test123"
}

def create_gradio_interface():
    manager = AgentManager()

    with gr.Blocks(title="AI Learning Assistant") as demo:
        auth_state = gr.State(False)  # store login status

        with gr.Column(visible=True) as login_section:
            gr.Markdown("## 🔐 Login to Continue")
            username = gr.Textbox(label="Username")
            password = gr.Textbox(label="Password", type="password")
            login_btn = gr.Button("Login")
            login_error = gr.Markdown(visible=False)

        with gr.Column(visible=False) as app_section:
            gr.Markdown("# 🧠 Learn with LlamaIndex Tools")

            with gr.Row():
                session_id = gr.Textbox(label="Session ID", value=str(uuid.uuid4()), visible=True)
                llm_selector = gr.Dropdown(
                    label="LLM Type",
                    choices=["Google", "OpenAI", "HuggingFace", "Mistral"],
                    value="Google",
                    interactive=True
                )

            with gr.Row():
                with gr.Column(scale=3):
                    chatbot = gr.Chatbot(label="Learning Dialog", height=500, type="messages")
                    query_input = gr.Textbox(label="Your Learning Query", placeholder="Ask about ML/DL algorithms...")
                    submit_btn = gr.Button("Submit")

                with gr.Column(scale=1):
                    gr.Markdown("### Tools Preview")
                    tool_output = gr.Textbox(label="Selected Tools", interactive=False)
                    response_output = gr.Textbox(label="Full Response", interactive=False, lines=10)

            def process_query(session_id_val, query, chat_history, llm_val):
                agent = manager.get_agent(session_id_val, llm_type=llm_val)
                chat_history, tools_used, response = agent.process_query(query)
                manager.save_agent(session_id_val, agent)
                return chat_history, tools_used, response, ""

            submit_btn.click(
                process_query,
                inputs=[session_id, query_input, chatbot, llm_selector],
                outputs=[chatbot, tool_output, response_output, query_input]
            )

            def load_history(session_id_val, llm_val):
                agent = manager.get_agent(session_id_val, llm_val)
                return agent.chat_history

            session_id.change(
                load_history,
                inputs=[session_id, llm_selector],
                outputs=chatbot
            )

        def check_login(username, password):
            if username == "admin" and password == "123":
                return "", True
            else:
                return "Invalid credentials", False

        login_btn.click(
            check_login,
            inputs=[username, password],
            outputs=[login_error, auth_state]
        )

        def toggle_ui(is_logged_in):
            return (
                gr.update(visible=is_logged_in),  # app_section
                gr.update(visible=not is_logged_in)  # login_section
            )

        auth_state.change(
            toggle_ui,
            inputs=auth_state,
            outputs=[app_section, login_section]
        )

    return demo

if __name__ == "__main__":
    interface = create_gradio_interface()
    interface.launch()