Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +53 -77
src/streamlit_app.py
CHANGED
|
@@ -5,44 +5,62 @@ from docx import Document
|
|
| 5 |
from docx.shared import Pt
|
| 6 |
import os
|
| 7 |
import tempfile
|
| 8 |
-
import asyncio
|
| 9 |
|
| 10 |
-
#
|
| 11 |
load_dotenv()
|
| 12 |
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 13 |
|
| 14 |
-
#
|
| 15 |
st.set_page_config(page_title="Claude UI-HF", layout="wide")
|
| 16 |
st.markdown("<h2 style='text-align:center;'>Claude UI-HF</h2>", unsafe_allow_html=True)
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def stream_claude_response(prompt, text):
|
| 20 |
if not prompt.strip() and not text.strip():
|
| 21 |
st.warning("β οΈ Please enter a prompt or text.")
|
| 22 |
return ""
|
| 23 |
|
| 24 |
-
user_content =
|
| 25 |
-
if prompt.strip():
|
| 26 |
-
user_content += prompt.strip()
|
| 27 |
if text.strip():
|
| 28 |
user_content += f"\n\n{text.strip()}"
|
| 29 |
|
| 30 |
output_text = ""
|
| 31 |
progress_bar = st.progress(0, text="Starting...")
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
output_box.text_area(
|
| 39 |
-
"Claude Output",
|
| 40 |
-
value=st.session_state.output_box_text,
|
| 41 |
-
height=400,
|
| 42 |
-
key="output_box_display",
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Streaming response
|
| 46 |
with client.messages.stream(
|
| 47 |
model="claude-sonnet-4-20250514",
|
| 48 |
max_tokens=64000,
|
|
@@ -52,76 +70,35 @@ def stream_claude_response(prompt, text):
|
|
| 52 |
if event.type == "content_block_delta" and event.delta.type == "text_delta":
|
| 53 |
delta = event.delta.text
|
| 54 |
output_text += delta
|
| 55 |
-
|
|
|
|
| 56 |
progress_bar.progress(min(i / 400, 0.95), text="Generating...")
|
| 57 |
-
output_box.text_area(
|
| 58 |
-
"Claude Output",
|
| 59 |
-
value=st.session_state.output_box_text,
|
| 60 |
-
height=400,
|
| 61 |
-
key="output_box_display",
|
| 62 |
-
)
|
| 63 |
|
| 64 |
progress_bar.progress(1.0, text="β
Done")
|
| 65 |
return output_text
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
line for line in text.splitlines() if line.strip() != "---"
|
| 72 |
-
).strip()
|
| 73 |
-
|
| 74 |
-
if not doc_name.strip():
|
| 75 |
-
doc_name = "Claude_Output"
|
| 76 |
-
docx_path = os.path.join(tempfile.gettempdir(), f"{doc_name}.docx")
|
| 77 |
-
|
| 78 |
-
document = Document()
|
| 79 |
-
for line in cleaned_output.split("\n"):
|
| 80 |
-
clean_line = line.strip()
|
| 81 |
-
if not clean_line:
|
| 82 |
-
continue
|
| 83 |
-
if clean_line.startswith("# "):
|
| 84 |
-
document.add_heading(clean_line[2:], level=1)
|
| 85 |
-
elif clean_line.startswith("## "):
|
| 86 |
-
document.add_heading(clean_line[3:], level=2)
|
| 87 |
-
elif clean_line.startswith("### "):
|
| 88 |
-
document.add_heading(clean_line[4:], level=3)
|
| 89 |
-
else:
|
| 90 |
-
para = document.add_paragraph(clean_line)
|
| 91 |
-
para.style.font.size = Pt(11)
|
| 92 |
-
document.save(docx_path)
|
| 93 |
-
return docx_path
|
| 94 |
-
|
| 95 |
-
# === Word Counter ===
|
| 96 |
-
def count_words(text):
|
| 97 |
-
words = len(text.split())
|
| 98 |
-
chars = len(text)
|
| 99 |
-
return f"π Words: {words:,} | Characters: {chars:,}"
|
| 100 |
-
|
| 101 |
-
# === UI Layout ===
|
| 102 |
-
with st.container():
|
| 103 |
-
prompt = st.text_area("Prompt", placeholder="Enter your instruction here", height=100)
|
| 104 |
-
text = st.text_area("Text", placeholder="Paste your large text here", height=300)
|
| 105 |
-
doc_name = st.text_input("π Document Name", placeholder="e.g. MyNotes")
|
| 106 |
|
| 107 |
run_col, cancel_col = st.columns([1, 1])
|
| 108 |
-
run_clicked = run_col.button("π Run"
|
| 109 |
-
cancel_clicked = cancel_col.button("π Cancel"
|
| 110 |
|
| 111 |
-
output_box = st.empty()
|
| 112 |
stats_placeholder = st.empty()
|
| 113 |
download_placeholder = st.empty()
|
|
|
|
| 114 |
|
| 115 |
-
#
|
| 116 |
if run_clicked:
|
| 117 |
try:
|
| 118 |
-
|
| 119 |
-
response_text = stream_claude_response(prompt, text)
|
| 120 |
if response_text:
|
| 121 |
-
# Word
|
| 122 |
stats_placeholder.write(count_words(response_text))
|
| 123 |
|
| 124 |
-
# Save DOCX
|
| 125 |
docx_file = create_docx(response_text, doc_name)
|
| 126 |
with open(docx_file, "rb") as f:
|
| 127 |
download_placeholder.download_button(
|
|
@@ -132,8 +109,8 @@ if run_clicked:
|
|
| 132 |
use_container_width=True
|
| 133 |
)
|
| 134 |
|
| 135 |
-
# Copy to clipboard (
|
| 136 |
-
|
| 137 |
<button onclick="navigator.clipboard.writeText(document.querySelector('textarea[data-testid=stTextArea-input]').value); alert('β
Copied to clipboard!');">
|
| 138 |
π Copy Output
|
| 139 |
</button>
|
|
@@ -143,5 +120,4 @@ if run_clicked:
|
|
| 143 |
st.error(f"β Error: {e}")
|
| 144 |
|
| 145 |
elif cancel_clicked:
|
| 146 |
-
st.warning("π Cancel clicked β refresh to restart
|
| 147 |
-
|
|
|
|
| 5 |
from docx.shared import Pt
|
| 6 |
import os
|
| 7 |
import tempfile
|
|
|
|
| 8 |
|
| 9 |
+
# ===== Load API Key =====
|
| 10 |
load_dotenv()
|
| 11 |
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 12 |
|
| 13 |
+
# ===== Page Config =====
|
| 14 |
st.set_page_config(page_title="Claude UI-HF", layout="wide")
|
| 15 |
st.markdown("<h2 style='text-align:center;'>Claude UI-HF</h2>", unsafe_allow_html=True)
|
| 16 |
|
| 17 |
+
# ===== Word/Character Counter =====
|
| 18 |
+
def count_words(text):
|
| 19 |
+
words = len(text.split())
|
| 20 |
+
chars = len(text)
|
| 21 |
+
return f"π Words: {words:,} | Characters: {chars:,}"
|
| 22 |
+
|
| 23 |
+
# ===== DOCX Creation =====
|
| 24 |
+
def create_docx(text, doc_name):
|
| 25 |
+
cleaned_output = "\n".join(line for line in text.splitlines() if line.strip() != "---").strip()
|
| 26 |
+
if not doc_name.strip():
|
| 27 |
+
doc_name = "Claude_Output"
|
| 28 |
+
docx_path = os.path.join(tempfile.gettempdir(), f"{doc_name}.docx")
|
| 29 |
+
document = Document()
|
| 30 |
+
for line in cleaned_output.split("\n"):
|
| 31 |
+
clean_line = line.strip()
|
| 32 |
+
if not clean_line:
|
| 33 |
+
continue
|
| 34 |
+
if clean_line.startswith("# "):
|
| 35 |
+
document.add_heading(clean_line[2:], level=1)
|
| 36 |
+
elif clean_line.startswith("## "):
|
| 37 |
+
document.add_heading(clean_line[3:], level=2)
|
| 38 |
+
elif clean_line.startswith("### "):
|
| 39 |
+
document.add_heading(clean_line[4:], level=3)
|
| 40 |
+
else:
|
| 41 |
+
para = document.add_paragraph(clean_line)
|
| 42 |
+
para.style.font.size = Pt(11)
|
| 43 |
+
document.save(docx_path)
|
| 44 |
+
return docx_path
|
| 45 |
+
|
| 46 |
+
# ===== Main Streaming Function =====
|
| 47 |
def stream_claude_response(prompt, text):
|
| 48 |
if not prompt.strip() and not text.strip():
|
| 49 |
st.warning("β οΈ Please enter a prompt or text.")
|
| 50 |
return ""
|
| 51 |
|
| 52 |
+
user_content = prompt.strip()
|
|
|
|
|
|
|
| 53 |
if text.strip():
|
| 54 |
user_content += f"\n\n{text.strip()}"
|
| 55 |
|
| 56 |
output_text = ""
|
| 57 |
progress_bar = st.progress(0, text="Starting...")
|
| 58 |
|
| 59 |
+
# Single placeholder for output
|
| 60 |
+
output_placeholder = st.empty()
|
| 61 |
+
output_placeholder.text_area("Claude Output", value="", height=400, key="output_box")
|
| 62 |
|
| 63 |
+
# Streaming from Claude
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with client.messages.stream(
|
| 65 |
model="claude-sonnet-4-20250514",
|
| 66 |
max_tokens=64000,
|
|
|
|
| 70 |
if event.type == "content_block_delta" and event.delta.type == "text_delta":
|
| 71 |
delta = event.delta.text
|
| 72 |
output_text += delta
|
| 73 |
+
# Update output placeholder
|
| 74 |
+
output_placeholder.text_area("Claude Output", value=output_text, height=400)
|
| 75 |
progress_bar.progress(min(i / 400, 0.95), text="Generating...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
progress_bar.progress(1.0, text="β
Done")
|
| 78 |
return output_text
|
| 79 |
|
| 80 |
+
# ===== UI Inputs =====
|
| 81 |
+
prompt = st.text_area("Prompt", placeholder="Enter your instruction here", height=100)
|
| 82 |
+
text = st.text_area("Text", placeholder="Paste your large text here", height=300)
|
| 83 |
+
doc_name = st.text_input("π Document Name", placeholder="e.g. MyNotes")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
run_col, cancel_col = st.columns([1, 1])
|
| 86 |
+
run_clicked = run_col.button("π Run")
|
| 87 |
+
cancel_clicked = cancel_col.button("π Cancel")
|
| 88 |
|
|
|
|
| 89 |
stats_placeholder = st.empty()
|
| 90 |
download_placeholder = st.empty()
|
| 91 |
+
copy_placeholder = st.empty()
|
| 92 |
|
| 93 |
+
# ===== Run Logic =====
|
| 94 |
if run_clicked:
|
| 95 |
try:
|
| 96 |
+
response_text = stream_claude_response(prompt, text)
|
|
|
|
| 97 |
if response_text:
|
| 98 |
+
# Word/char stats
|
| 99 |
stats_placeholder.write(count_words(response_text))
|
| 100 |
|
| 101 |
+
# Save DOCX & provide download
|
| 102 |
docx_file = create_docx(response_text, doc_name)
|
| 103 |
with open(docx_file, "rb") as f:
|
| 104 |
download_placeholder.download_button(
|
|
|
|
| 109 |
use_container_width=True
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Copy to clipboard (JS)
|
| 113 |
+
copy_placeholder.markdown("""
|
| 114 |
<button onclick="navigator.clipboard.writeText(document.querySelector('textarea[data-testid=stTextArea-input]').value); alert('β
Copied to clipboard!');">
|
| 115 |
π Copy Output
|
| 116 |
</button>
|
|
|
|
| 120 |
st.error(f"β Error: {e}")
|
| 121 |
|
| 122 |
elif cancel_clicked:
|
| 123 |
+
st.warning("π Cancel clicked β refresh the page to restart.")
|
|
|