Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from trykarenge/trykarneka: direct link, hf CLI and curl.
- Browser
- Download file 7.47 kB
-
https://huggingface.co/spaces/trykarenge/trykarneka/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/trykarenge/trykarneka/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/trykarenge/trykarneka/resolve/main/src/streamlit_app.py
7.47 kB
| import streamlit as st | |
| from openai import OpenAI | |
| # ----------------------------- | |
| # Page Configuration | |
| # ----------------------------- | |
| st.set_page_config( | |
| page_title="AI Developer Playground", | |
| layout="wide" | |
| ) | |
| # ----------------------------- | |
| # Sidebar | |
| # ----------------------------- | |
| st.sidebar.title("π Setup") | |
| api_key = st.sidebar.text_input( | |
| "Enter your OpenAI API Key", | |
| type="password" | |
| ) | |
| # ----------------------------- | |
| # Validate API Key | |
| # ----------------------------- | |
| if api_key: | |
| api_key = api_key.strip() | |
| try: | |
| client = OpenAI(api_key=api_key) | |
| st.sidebar.success("API Key Loaded Successfully") | |
| # Optional API Test Button | |
| if st.sidebar.button("Test API Key"): | |
| try: | |
| test_response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": "Hello" | |
| } | |
| ] | |
| ) | |
| st.sidebar.success("API Key Works!") | |
| except Exception as e: | |
| st.sidebar.error(f"API Test Failed: {str(e)}") | |
| # ----------------------------- | |
| # Main App | |
| # ----------------------------- | |
| st.title("π€ AI Developer Playground") | |
| st.write( | |
| "Experiment with AI chatbots, code generation, and document Q&A using OpenAI models." | |
| ) | |
| # ----------------------------- | |
| # Tabs | |
| # ----------------------------- | |
| tab1, tab2, tab3 = st.tabs( | |
| ["π¬ Chatbot", "π» Code Generator", "π Docs Q&A"] | |
| ) | |
| # ===================================================== | |
| # TAB 1 β CHATBOT | |
| # ===================================================== | |
| with tab1: | |
| st.header("π¬ Chat with AI") | |
| user_input = st.text_area( | |
| "Your message:", | |
| height=150 | |
| ) | |
| if st.button("Send", key="chat_button"): | |
| if user_input.strip(): | |
| with st.spinner("Thinking..."): | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": user_input | |
| } | |
| ] | |
| ) | |
| answer = response.choices[0].message.content | |
| st.success(answer) | |
| except Exception as e: | |
| st.error(f"OpenAI Error: {str(e)}") | |
| else: | |
| st.warning("Please enter a message.") | |
| # ===================================================== | |
| # TAB 2 β CODE GENERATOR | |
| # ===================================================== | |
| with tab2: | |
| st.header("π» AI Code Assistant") | |
| code_prompt = st.text_area( | |
| "Describe the code you need:", | |
| height=150 | |
| ) | |
| if st.button("Generate Code", key="code_button"): | |
| if code_prompt.strip(): | |
| with st.spinner("Generating code..."): | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": ( | |
| "You are an expert software engineer. " | |
| "Generate clean and production-ready code." | |
| ) | |
| }, | |
| { | |
| "role": "user", | |
| "content": code_prompt | |
| } | |
| ] | |
| ) | |
| generated_code = response.choices[0].message.content | |
| st.code(generated_code, language="python") | |
| except Exception as e: | |
| st.error(f"OpenAI Error: {str(e)}") | |
| else: | |
| st.warning("Please describe the code you need.") | |
| # ===================================================== | |
| # TAB 3 β DOCS Q&A | |
| # ===================================================== | |
| with tab3: | |
| st.header("π Ask Questions About Your Documents") | |
| uploaded_file = st.file_uploader( | |
| "Upload a TXT or Markdown file", | |
| type=["txt", "md"] | |
| ) | |
| if uploaded_file is not None: | |
| try: | |
| docs_text = uploaded_file.getvalue().decode("utf-8") | |
| st.success(f"Uploaded file: {uploaded_file.name}") | |
| st.write(f"Document size: {len(docs_text)} characters") | |
| question = st.text_input( | |
| "Ask a question about the document:" | |
| ) | |
| if st.button("Get Answer", key="docs_button"): | |
| if question.strip(): | |
| with st.spinner("Analyzing document..."): | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": ( | |
| "You are a helpful assistant. " | |
| "Answer questions only using the " | |
| "provided document.\n\n" | |
| f"DOCUMENT:\n{docs_text}" | |
| ) | |
| }, | |
| { | |
| "role": "user", | |
| "content": question | |
| } | |
| ] | |
| ) | |
| answer = response.choices[0].message.content | |
| st.success(answer) | |
| except Exception as e: | |
| st.error(f"OpenAI Error: {str(e)}") | |
| else: | |
| st.warning("Please enter a question.") | |
| except Exception as e: | |
| st.error(f"File Processing Error: {str(e)}") | |
| except Exception as e: | |
| st.sidebar.error(f"Invalid API Key: {str(e)}") | |
| else: | |
| st.warning( | |
| "Please enter your OpenAI API Key in the sidebar to start." | |
| ) |