Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,13 +12,14 @@ load_dotenv()
|
|
| 12 |
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 13 |
|
| 14 |
# === Async Streaming Function ===
|
| 15 |
-
async def query_claude_stream(prompt, text, doc_name, progress=
|
| 16 |
if not prompt.strip() and not text.strip():
|
| 17 |
yield "⚠️ Please enter a prompt or text.", None
|
| 18 |
return
|
| 19 |
|
| 20 |
output_text = ""
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
user_content = ""
|
| 24 |
if prompt.strip():
|
|
@@ -41,10 +42,12 @@ async def query_claude_stream(prompt, text, doc_name, progress=gr.Progress()):
|
|
| 41 |
async for delta in _to_async_generator(_run_stream()):
|
| 42 |
i += 1
|
| 43 |
output_text += delta
|
| 44 |
-
|
|
|
|
| 45 |
yield output_text, None
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
cleaned_output = "\n".join(
|
| 50 |
line for line in output_text.splitlines() if line.strip() != "---"
|
|
@@ -71,7 +74,6 @@ async def query_claude_stream(prompt, text, doc_name, progress=gr.Progress()):
|
|
| 71 |
para.style.font.size = Pt(11)
|
| 72 |
|
| 73 |
document.save(docx_path)
|
| 74 |
-
|
| 75 |
yield cleaned_output, docx_path
|
| 76 |
|
| 77 |
|
|
@@ -89,15 +91,8 @@ def count_words(text):
|
|
| 89 |
return f"📊 Words: {words:,} | Characters: {chars:,}"
|
| 90 |
|
| 91 |
|
| 92 |
-
# === Gradio UI (
|
| 93 |
-
with gr.Blocks(
|
| 94 |
-
#output_box textarea {
|
| 95 |
-
overflow-y: auto !important;
|
| 96 |
-
height: 400px !important;
|
| 97 |
-
resize: vertical !important;
|
| 98 |
-
font-family: monospace;
|
| 99 |
-
}
|
| 100 |
-
""") as demo:
|
| 101 |
|
| 102 |
gr.Markdown("<h2 style='text-align:center;'>Claude UI-HF</h2>")
|
| 103 |
|
|
@@ -112,7 +107,6 @@ with gr.Blocks(css="""
|
|
| 112 |
output = gr.Textbox(
|
| 113 |
label="Claude Output",
|
| 114 |
lines=15,
|
| 115 |
-
elem_id="output_box",
|
| 116 |
interactive=True
|
| 117 |
)
|
| 118 |
|
|
@@ -135,7 +129,7 @@ with gr.Blocks(css="""
|
|
| 135 |
# Auto word counter
|
| 136 |
output.change(count_words, inputs=output, outputs=stats)
|
| 137 |
|
| 138 |
-
# Copy button
|
| 139 |
copy_btn.click(
|
| 140 |
lambda x: x,
|
| 141 |
inputs=output,
|
|
@@ -145,6 +139,6 @@ with gr.Blocks(css="""
|
|
| 145 |
)
|
| 146 |
|
| 147 |
|
| 148 |
-
#
|
| 149 |
demo.queue()
|
| 150 |
-
demo.launch(
|
|
|
|
| 12 |
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 13 |
|
| 14 |
# === Async Streaming Function ===
|
| 15 |
+
async def query_claude_stream(prompt, text, doc_name, progress=None):
|
| 16 |
if not prompt.strip() and not text.strip():
|
| 17 |
yield "⚠️ Please enter a prompt or text.", None
|
| 18 |
return
|
| 19 |
|
| 20 |
output_text = ""
|
| 21 |
+
if progress:
|
| 22 |
+
progress(0)
|
| 23 |
|
| 24 |
user_content = ""
|
| 25 |
if prompt.strip():
|
|
|
|
| 42 |
async for delta in _to_async_generator(_run_stream()):
|
| 43 |
i += 1
|
| 44 |
output_text += delta
|
| 45 |
+
if progress:
|
| 46 |
+
progress(min(0.95, i / 500))
|
| 47 |
yield output_text, None
|
| 48 |
|
| 49 |
+
if progress:
|
| 50 |
+
progress(1)
|
| 51 |
|
| 52 |
cleaned_output = "\n".join(
|
| 53 |
line for line in output_text.splitlines() if line.strip() != "---"
|
|
|
|
| 74 |
para.style.font.size = Pt(11)
|
| 75 |
|
| 76 |
document.save(docx_path)
|
|
|
|
| 77 |
yield cleaned_output, docx_path
|
| 78 |
|
| 79 |
|
|
|
|
| 91 |
return f"📊 Words: {words:,} | Characters: {chars:,}"
|
| 92 |
|
| 93 |
|
| 94 |
+
# === Gradio UI (Backward-compatible: no css, no theme) ===
|
| 95 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
gr.Markdown("<h2 style='text-align:center;'>Claude UI-HF</h2>")
|
| 98 |
|
|
|
|
| 107 |
output = gr.Textbox(
|
| 108 |
label="Claude Output",
|
| 109 |
lines=15,
|
|
|
|
| 110 |
interactive=True
|
| 111 |
)
|
| 112 |
|
|
|
|
| 129 |
# Auto word counter
|
| 130 |
output.change(count_words, inputs=output, outputs=stats)
|
| 131 |
|
| 132 |
+
# Copy button
|
| 133 |
copy_btn.click(
|
| 134 |
lambda x: x,
|
| 135 |
inputs=output,
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
|
| 142 |
+
# Launch app (works on any Gradio >=3.x)
|
| 143 |
demo.queue()
|
| 144 |
+
demo.launch()
|