Spaces:
Sleeping
Sleeping
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() | |