Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +129 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,131 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from anthropic import Anthropic
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from docx import Document
|
| 5 |
+
from docx.shared import Pt
|
| 6 |
+
import os
|
| 7 |
+
import tempfile
|
| 8 |
+
import asyncio
|
| 9 |
+
|
| 10 |
+
# === Load API Key ===
|
| 11 |
+
load_dotenv()
|
| 12 |
+
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 13 |
+
|
| 14 |
+
# === Page Config ===
|
| 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 |
+
# === Helper: Stream Claude Response ===
|
| 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 |
+
progress_step = 0
|
| 33 |
+
|
| 34 |
+
# Streaming response
|
| 35 |
+
with client.messages.stream(
|
| 36 |
+
model="claude-sonnet-4-20250514",
|
| 37 |
+
max_tokens=64000,
|
| 38 |
+
messages=[{"role": "user", "content": user_content}],
|
| 39 |
+
) as stream:
|
| 40 |
+
placeholder = st.empty()
|
| 41 |
+
for event in stream:
|
| 42 |
+
if event.type == "content_block_delta" and event.delta.type == "text_delta":
|
| 43 |
+
delta = event.delta.text
|
| 44 |
+
output_text += delta
|
| 45 |
+
progress_step += 1
|
| 46 |
+
progress_bar.progress(min(progress_step / 400, 0.95), text="Generating...")
|
| 47 |
+
placeholder.text_area("Claude Output", value=output_text, height=400, key="output_box", label_visibility="collapsed")
|
| 48 |
+
|
| 49 |
+
progress_bar.progress(1.0, text="β
Done")
|
| 50 |
+
return output_text
|
| 51 |
+
|
| 52 |
+
# === DOCX Creation ===
|
| 53 |
+
def create_docx(text, doc_name):
|
| 54 |
+
cleaned_output = "\n".join(
|
| 55 |
+
line for line in text.splitlines() if line.strip() != "---"
|
| 56 |
+
).strip()
|
| 57 |
+
|
| 58 |
+
if not doc_name.strip():
|
| 59 |
+
doc_name = "Claude_Output"
|
| 60 |
+
docx_path = os.path.join(tempfile.gettempdir(), f"{doc_name}.docx")
|
| 61 |
+
|
| 62 |
+
document = Document()
|
| 63 |
+
for line in cleaned_output.split("\n"):
|
| 64 |
+
clean_line = line.strip()
|
| 65 |
+
if not clean_line:
|
| 66 |
+
continue
|
| 67 |
+
if clean_line.startswith("# "):
|
| 68 |
+
document.add_heading(clean_line[2:], level=1)
|
| 69 |
+
elif clean_line.startswith("## "):
|
| 70 |
+
document.add_heading(clean_line[3:], level=2)
|
| 71 |
+
elif clean_line.startswith("### "):
|
| 72 |
+
document.add_heading(clean_line[4:], level=3)
|
| 73 |
+
else:
|
| 74 |
+
para = document.add_paragraph(clean_line)
|
| 75 |
+
para.style.font.size = Pt(11)
|
| 76 |
+
document.save(docx_path)
|
| 77 |
+
return docx_path
|
| 78 |
+
|
| 79 |
+
# === Word Counter ===
|
| 80 |
+
def count_words(text):
|
| 81 |
+
words = len(text.split())
|
| 82 |
+
chars = len(text)
|
| 83 |
+
return f"π Words: {words:,} | Characters: {chars:,}"
|
| 84 |
+
|
| 85 |
+
# === UI Layout ===
|
| 86 |
+
with st.container():
|
| 87 |
+
prompt = st.text_area("Prompt", placeholder="Enter your instruction here", height=100)
|
| 88 |
+
text = st.text_area("Text", placeholder="Paste your large text here", height=300)
|
| 89 |
+
doc_name = st.text_input("π Document Name", placeholder="e.g. MyNotes")
|
| 90 |
+
|
| 91 |
+
run_col, cancel_col = st.columns([1, 1])
|
| 92 |
+
run_clicked = run_col.button("π Run", use_container_width=True)
|
| 93 |
+
cancel_clicked = cancel_col.button("π Cancel", use_container_width=True)
|
| 94 |
+
|
| 95 |
+
output_box = st.empty()
|
| 96 |
+
stats_placeholder = st.empty()
|
| 97 |
+
download_placeholder = st.empty()
|
| 98 |
+
|
| 99 |
+
# === Main Logic ===
|
| 100 |
+
if run_clicked:
|
| 101 |
+
try:
|
| 102 |
+
with st.spinner("Running Claude..."):
|
| 103 |
+
response_text = stream_claude_response(prompt, text)
|
| 104 |
+
if response_text:
|
| 105 |
+
# Word count
|
| 106 |
+
stats_placeholder.write(count_words(response_text))
|
| 107 |
+
|
| 108 |
+
# Save DOCX
|
| 109 |
+
docx_file = create_docx(response_text, doc_name)
|
| 110 |
+
with open(docx_file, "rb") as f:
|
| 111 |
+
download_placeholder.download_button(
|
| 112 |
+
label="β¬οΈ Download DOCX",
|
| 113 |
+
data=f,
|
| 114 |
+
file_name=f"{doc_name or 'Claude_Output'}.docx",
|
| 115 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 116 |
+
use_container_width=True
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Copy to clipboard (via JS)
|
| 120 |
+
st.markdown("""
|
| 121 |
+
<button onclick="navigator.clipboard.writeText(document.querySelector('textarea[data-testid=stTextArea-input]').value); alert('β
Copied to clipboard!');">
|
| 122 |
+
π Copy Output
|
| 123 |
+
</button>
|
| 124 |
+
""", unsafe_allow_html=True)
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(f"β Error: {e}")
|
| 128 |
+
|
| 129 |
+
elif cancel_clicked:
|
| 130 |
+
st.warning("π Cancel clicked β refresh to restart session.")
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|