Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
|
|
| 10 |
from docx import Document as DocxDocument
|
| 11 |
import gradio as gr
|
| 12 |
from langchain_community.vectorstores import FAISS
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# -------------------------------
|
| 15 |
# 2. 環境變數與資料路徑
|
|
@@ -81,8 +83,9 @@ def get_hf_rate_limit():
|
|
| 81 |
except Exception:
|
| 82 |
return "無法取得 API 速率資訊"
|
| 83 |
|
|
|
|
| 84 |
# -------------------------------
|
| 85 |
-
# 6. 生成文章(進度條 +
|
| 86 |
# -------------------------------
|
| 87 |
def generate_article_with_progress_bar(query, segments=5):
|
| 88 |
docx_file = "/tmp/generated_article.docx"
|
|
@@ -105,8 +108,10 @@ def generate_article_with_progress_bar(query, segments=5):
|
|
| 105 |
except Exception as e:
|
| 106 |
paragraph = f"(本段生成失敗:{e})"
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
prompt = f"請接續上一段生成下一段:\n{paragraph}\n\n下一段:"
|
| 111 |
|
| 112 |
# 更新進度
|
|
@@ -119,6 +124,7 @@ def generate_article_with_progress_bar(query, segments=5):
|
|
| 119 |
full_text = "\n\n".join(all_text)
|
| 120 |
yield 100, gr.Textbox.update(value=f"{rate_info}\n\n{full_text}"), docx_file
|
| 121 |
|
|
|
|
| 122 |
# -------------------------------
|
| 123 |
# 7. Gradio 介面(進度條 + 文字 + 文件)
|
| 124 |
# -------------------------------
|
|
|
|
| 10 |
from docx import Document as DocxDocument
|
| 11 |
import gradio as gr
|
| 12 |
from langchain_community.vectorstores import FAISS
|
| 13 |
+
import textwrap
|
| 14 |
+
|
| 15 |
|
| 16 |
# -------------------------------
|
| 17 |
# 2. 環境變數與資料路徑
|
|
|
|
| 83 |
except Exception:
|
| 84 |
return "無法取得 API 速率資訊"
|
| 85 |
|
| 86 |
+
|
| 87 |
# -------------------------------
|
| 88 |
+
# 6. 生成文章(進度條 + 美化換行)
|
| 89 |
# -------------------------------
|
| 90 |
def generate_article_with_progress_bar(query, segments=5):
|
| 91 |
docx_file = "/tmp/generated_article.docx"
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
paragraph = f"(本段生成失敗:{e})"
|
| 110 |
|
| 111 |
+
# 美化換行,每行約 60 字
|
| 112 |
+
wrapped_paragraph = "\n".join(textwrap.wrap(paragraph, width=60))
|
| 113 |
+
all_text.append(wrapped_paragraph)
|
| 114 |
+
doc.add_paragraph(paragraph) # DOCX 保留原文,不換行
|
| 115 |
prompt = f"請接續上一段生成下一段:\n{paragraph}\n\n下一段:"
|
| 116 |
|
| 117 |
# 更新進度
|
|
|
|
| 124 |
full_text = "\n\n".join(all_text)
|
| 125 |
yield 100, gr.Textbox.update(value=f"{rate_info}\n\n{full_text}"), docx_file
|
| 126 |
|
| 127 |
+
|
| 128 |
# -------------------------------
|
| 129 |
# 7. Gradio 介面(進度條 + 文字 + 文件)
|
| 130 |
# -------------------------------
|