File size: 4,474 Bytes
ad7058b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df73cbe
ad7058b
 
 
 
 
df73cbe
ad7058b
 
 
 
df73cbe
 
 
 
 
 
 
 
 
 
 
ad7058b
 
 
df73cbe
 
 
 
 
 
 
ad7058b
df73cbe
 
ad7058b
 
 
df73cbe
 
 
ad7058b
 
df73cbe
ad7058b
 
 
df73cbe
 
ad7058b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df73cbe
ad7058b
 
 
 
df73cbe
 
 
 
 
 
 
 
 
ad7058b
 
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
103
104
105
106
107
108
import os
import streamlit as st
from openai import OpenAI
from conversation_manager import ConversationManager

api_key = os.getenv("NVIDIA_API_KEY")

# Check if the API key is available
if api_key is None:
    st.error("NVIDIA_API_KEY environment variable not found.")
else:
    client = OpenAI(
        base_url="https://integrate.api.nvidia.com/v1",
        api_key=api_key
    )
    # Initialize the conversation manager
    if "conversation_manager" not in st.session_state:
        st.session_state.conversation_manager = ConversationManager(client)

def main():
    st.title("Django Skills Builder")
    st.write("Learn Django by generating step-by-step tutorials for specific programming tasks.")

    # Tabs for intuitive navigation
    tab1, tab2, tab3, tab4 = st.tabs(["About", "Task Selection", "Conversation History", "Settings"])

    with tab1:
        st.header("About")
        st.write("This app helps you learn Django by generating detailed step-by-step tutorials for various tasks. "
                 "Simply select a task, edit the generated prompt if necessary, and the app will create a comprehensive tutorial to guide you.")

    with tab2:
        st.header("Task Selection")
        task_options = [
            "Create a new Django project",
            "Create a Django app",
            "Define Django models",
            "Create Django views",
            "Create Django templates",
            "Configure Django admin",
            "Define Django URLs",
            "Add Django forms",
            "Add Django authentication",
            "Add Django REST framework",
            "Deploy a Django project to fly.io"
        ]
        selected_task = st.selectbox("Choose a task to learn:", task_options)

        # Generate a default prompt
        default_prompt = f"Create a detailed step-by-step tutorial on how to {selected_task.lower()} using the Django framework."
        edited_prompt = st.text_area("Edit the generated prompt before submission:", value=default_prompt, height=150)

        # Checkbox to enable/disable saving to history
        save_to_history = st.checkbox("Save this prompt to history", value=True)

        if st.button("Generate Tutorial"):
            user_prompt = edited_prompt.strip()  # Use the user-edited prompt
            st.write("**Final Prompt Submitted:**", user_prompt)

            with st.spinner("Generating tutorial..."):
                conversation_manager = st.session_state.conversation_manager

                if save_to_history:
                    conversation_manager.add_user_message(user_prompt)

                if conversation_manager.check_warning(max_prompts=4):
                    st.warning("The conversation history is getting long. Consider clearing or summarizing the history.")

                ai_response = conversation_manager.generate_ai_response(user_prompt)
                if ai_response:
                    if save_to_history:
                        conversation_manager.add_ai_message(ai_response)
                    st.markdown(f"**User:** {user_prompt}")
                    st.markdown(f"**AI:** {ai_response}")
                else:
                    st.write("**Error:** Failed to generate AI response.")

    with tab3:
        st.header("Conversation History")
        conversation_manager = st.session_state.conversation_manager
        if conversation_manager.conversation_history:
            for msg in conversation_manager.conversation_history:
                if msg['role'] == 'user':
                    st.markdown(f"**User:** {msg['content']}")
                else:
                    st.markdown(f"**AI:** {msg['content']}")
        else:
            st.write("No conversation history yet.")

    with tab4:
        st.header("Settings")
        
        if st.button("Clear History"):
            st.session_state.conversation_manager.clear_history()
            st.success("Conversation history cleared!")

        if st.button("Summarize History"):
            if not st.session_state.conversation_manager.conversation_history:
                st.warning("No conversation history to summarize.")
            else:
                with st.spinner("Summarizing conversation history..."):
                    summary = st.session_state.conversation_manager.summarize_history()
                    st.success("Conversation history summarized!")
                    st.write("**Summary:**", summary)

if __name__ == "__main__":
    main()