Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 1. Configuration
|
| 6 |
+
st.set_page_config(page_title="Executive Editor", layout="wide", page_icon="⚓")
|
| 7 |
+
|
| 8 |
+
# API Key Logic (Same as before)
|
| 9 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
try:
|
| 12 |
+
api_key = st.secrets["OPENAI_API_KEY"]
|
| 13 |
+
except (FileNotFoundError, KeyError):
|
| 14 |
+
st.error("OpenAI API Key not found.")
|
| 15 |
+
st.stop()
|
| 16 |
+
|
| 17 |
+
client = OpenAI(api_key=api_key)
|
| 18 |
+
|
| 19 |
+
# 2. Sidebar Controls (The "Executive Dashboard")
|
| 20 |
+
with st.sidebar:
|
| 21 |
+
st.header("🖊️ Editing Parameters")
|
| 22 |
+
|
| 23 |
+
# Mode Selection
|
| 24 |
+
edit_mode = st.radio(
|
| 25 |
+
"Optimization Goal",
|
| 26 |
+
[
|
| 27 |
+
"Standard Polish (Grammar & Flow)",
|
| 28 |
+
"BLUF (Bottom Line Up Front)",
|
| 29 |
+
"Diplomatic / De-escalate",
|
| 30 |
+
"Executive Summary (Concise)",
|
| 31 |
+
"Technical Precision"
|
| 32 |
+
]
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Output Format
|
| 36 |
+
format_mode = st.selectbox("Output Format", ["Plain Text", "Email Structure", "Memo Format", "Bullet Points"])
|
| 37 |
+
|
| 38 |
+
st.divider()
|
| 39 |
+
|
| 40 |
+
if st.button("🗑️ Clear Context", use_container_width=True):
|
| 41 |
+
st.session_state.messages = []
|
| 42 |
+
st.rerun()
|
| 43 |
+
|
| 44 |
+
# 3. The "Command" System Prompt
|
| 45 |
+
# Note the lower temperature and specific instructions for professional contexts.
|
| 46 |
+
SYSTEM_PROMPT = f"""
|
| 47 |
+
You are an expert Executive Editor and Technical Writer.
|
| 48 |
+
Your goal is to rewrite the user's input to be clear, concise, and professional.
|
| 49 |
+
|
| 50 |
+
CURRENT SETTINGS:
|
| 51 |
+
- **Goal:** {edit_mode}
|
| 52 |
+
- **Format:** {format_mode}
|
| 53 |
+
|
| 54 |
+
OPERATIONAL RULES:
|
| 55 |
+
1. **Direct Action:** Do not explain your changes unless asked. Just provide the rewritten text.
|
| 56 |
+
2. **Preserve Intent:** Never change the core meaning or facts of the input.
|
| 57 |
+
3. **No Fluff:** Remove adjectives and adverbs that do not add value.
|
| 58 |
+
4. **BLUF:** If the mode is "BLUF", ensure the main request or conclusion is the very first sentence.
|
| 59 |
+
5. **Diplomacy:** If the mode is "Diplomatic", strip out aggression/emotion and replace with professional firmness.
|
| 60 |
+
|
| 61 |
+
Start every response immediately with the rewritten text.
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
# 4. Session State
|
| 65 |
+
if "messages" not in st.session_state:
|
| 66 |
+
st.session_state.messages = []
|
| 67 |
+
|
| 68 |
+
# 5. Main Interface
|
| 69 |
+
st.title("⚓ Executive Editor")
|
| 70 |
+
st.markdown(f"**Current Mode:** `{edit_mode}`")
|
| 71 |
+
|
| 72 |
+
# Display History
|
| 73 |
+
for message in st.session_state.messages:
|
| 74 |
+
with st.chat_message(message["role"]):
|
| 75 |
+
st.markdown(message["content"])
|
| 76 |
+
|
| 77 |
+
# Input Handling
|
| 78 |
+
if user_input := st.chat_input("Paste text to edit, or type instructions..."):
|
| 79 |
+
|
| 80 |
+
# User Message
|
| 81 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 82 |
+
with st.chat_message("user"):
|
| 83 |
+
st.markdown(user_input)
|
| 84 |
+
|
| 85 |
+
# AI Response
|
| 86 |
+
with st.chat_message("assistant"):
|
| 87 |
+
message_placeholder = st.empty()
|
| 88 |
+
full_response = ""
|
| 89 |
+
|
| 90 |
+
# We re-inject the system prompt every turn to capture Sidebar changes
|
| 91 |
+
messages_payload = [{"role": "system", "content": SYSTEM_PROMPT}] + st.session_state.messages
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
response = client.chat.completions.create(
|
| 95 |
+
model="gpt-4o",
|
| 96 |
+
messages=messages_payload,
|
| 97 |
+
temperature=0.3, # Low temp = High consistency/Precision
|
| 98 |
+
stream=True
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
for chunk in response:
|
| 102 |
+
if chunk.choices[0].delta.content is not None:
|
| 103 |
+
full_response += chunk.choices[0].delta.content
|
| 104 |
+
message_placeholder.markdown(full_response + "▌")
|
| 105 |
+
|
| 106 |
+
message_placeholder.markdown(full_response)
|
| 107 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
st.error(f"Error: {e}")
|