Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,56 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 5 |
import json
|
| 6 |
|
| 7 |
-
|
| 8 |
-
DIFY_BASE_URL = os.environ.get("DIFY_BASE_URL", "")
|
| 9 |
-
DIFY_API_KEY_MYWORKFLOW = os.environ.get("DIFY_API_KEY_MYWORKFLOW", "")
|
| 10 |
-
|
| 11 |
-
def run_workflow(file):
|
| 12 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if not file:
|
| 14 |
return "PDFファイルを選択してください。", ""
|
| 15 |
-
|
|
|
|
| 16 |
loader = UnstructuredPDFLoader(file)
|
| 17 |
data = loader.load()
|
| 18 |
raw_text = data[0].page_content
|
| 19 |
-
|
|
|
|
| 20 |
inputs = {
|
| 21 |
"url": "",
|
| 22 |
"knowledge": raw_text
|
| 23 |
}
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
url = DIFY_BASE_URL + "/workflows/run"
|
|
|
|
|
|
|
| 27 |
headers = {
|
| 28 |
"Content-Type": "application/json",
|
| 29 |
"Authorization": f"Bearer {DIFY_API_KEY_MYWORKFLOW}"
|
| 30 |
}
|
|
|
|
|
|
|
| 31 |
data = {
|
| 32 |
"inputs": inputs,
|
| 33 |
"query": "",
|
| 34 |
"response_mode": "streaming",
|
| 35 |
"user": "abc_123",
|
| 36 |
}
|
|
|
|
|
|
|
| 37 |
response = requests.post(url, headers=headers, json=data, stream=True)
|
| 38 |
response.raise_for_status()
|
| 39 |
-
|
| 40 |
assistant_message = ""
|
| 41 |
outputs = {}
|
| 42 |
-
|
|
|
|
| 43 |
for chunk in response.iter_lines(delimiter=b"\n\n"):
|
| 44 |
if chunk:
|
| 45 |
chunk_data = chunk.decode("utf-8").strip()
|
|
@@ -50,39 +61,74 @@ def run_workflow(file):
|
|
| 50 |
if result.get("event") == "text_chunk":
|
| 51 |
answer = result.get("data", "").get("text", "")
|
| 52 |
assistant_message += str(answer)
|
| 53 |
-
yield raw_text,assistant_message,result.get("data", "")
|
| 54 |
elif result.get("event") == "workflow_finished":
|
| 55 |
outputs = result.get('data', "")
|
| 56 |
-
yield raw_text,assistant_message,outputs
|
| 57 |
-
|
| 58 |
-
# print(outputs.get('result', ""))
|
| 59 |
-
# print(outputs.get('row_content', ""))
|
| 60 |
-
# print(outputs.get('url', ""))
|
| 61 |
-
|
| 62 |
-
# return raw_text, assistant_message, outputs
|
| 63 |
except Exception as e:
|
| 64 |
error_message = str(e)
|
| 65 |
print(f"Error: {error_message}")
|
| 66 |
return "error", error_message, {}
|
| 67 |
|
|
|
|
| 68 |
iface = gr.Interface(
|
| 69 |
fn=run_workflow,
|
| 70 |
-
inputs=[gr.
|
| 71 |
outputs=[
|
| 72 |
gr.Textbox(label="生テキスト", show_copy_button=True, max_lines=5),
|
| 73 |
gr.Markdown(),
|
| 74 |
gr.JSON()
|
| 75 |
],
|
| 76 |
title="PDF to Dify Workflow",
|
| 77 |
-
description="PDFファイルを入力すると、Dify API
|
| 78 |
article="""
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
iface.queue().launch()
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
def run_workflow(message):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
+
file = message['files'][0]
|
| 10 |
+
text_message = message['text']
|
| 11 |
+
|
| 12 |
+
# PDFファイルが選択されているかチェック
|
| 13 |
if not file:
|
| 14 |
return "PDFファイルを選択してください。", ""
|
| 15 |
+
|
| 16 |
+
# PDFファイルをロードしてテキストを抽出
|
| 17 |
loader = UnstructuredPDFLoader(file)
|
| 18 |
data = loader.load()
|
| 19 |
raw_text = data[0].page_content
|
| 20 |
+
|
| 21 |
+
# APIリクエストのための入力データを準備
|
| 22 |
inputs = {
|
| 23 |
"url": "",
|
| 24 |
"knowledge": raw_text
|
| 25 |
}
|
| 26 |
+
|
| 27 |
+
yield raw_text, "loading...", {}
|
| 28 |
+
|
| 29 |
+
# APIエンドポイントURL
|
| 30 |
url = DIFY_BASE_URL + "/workflows/run"
|
| 31 |
+
|
| 32 |
+
# APIリクエストのヘッダー
|
| 33 |
headers = {
|
| 34 |
"Content-Type": "application/json",
|
| 35 |
"Authorization": f"Bearer {DIFY_API_KEY_MYWORKFLOW}"
|
| 36 |
}
|
| 37 |
+
|
| 38 |
+
# APIリクエストのデータ
|
| 39 |
data = {
|
| 40 |
"inputs": inputs,
|
| 41 |
"query": "",
|
| 42 |
"response_mode": "streaming",
|
| 43 |
"user": "abc_123",
|
| 44 |
}
|
| 45 |
+
|
| 46 |
+
# APIにリクエストを送信
|
| 47 |
response = requests.post(url, headers=headers, json=data, stream=True)
|
| 48 |
response.raise_for_status()
|
| 49 |
+
|
| 50 |
assistant_message = ""
|
| 51 |
outputs = {}
|
| 52 |
+
|
| 53 |
+
# APIレスポンスのチャンク処理
|
| 54 |
for chunk in response.iter_lines(delimiter=b"\n\n"):
|
| 55 |
if chunk:
|
| 56 |
chunk_data = chunk.decode("utf-8").strip()
|
|
|
|
| 61 |
if result.get("event") == "text_chunk":
|
| 62 |
answer = result.get("data", "").get("text", "")
|
| 63 |
assistant_message += str(answer)
|
| 64 |
+
yield raw_text, assistant_message, result.get("data", "")
|
| 65 |
elif result.get("event") == "workflow_finished":
|
| 66 |
outputs = result.get('data', "")
|
| 67 |
+
yield raw_text, assistant_message, outputs
|
| 68 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
error_message = str(e)
|
| 71 |
print(f"Error: {error_message}")
|
| 72 |
return "error", error_message, {}
|
| 73 |
|
| 74 |
+
# Gradioインターフェイスの設定
|
| 75 |
iface = gr.Interface(
|
| 76 |
fn=run_workflow,
|
| 77 |
+
inputs=[gr.MultimodalTextbox(label="PDFファイルをアップロード", file_types=[".pdf"], interactive=True)],
|
| 78 |
outputs=[
|
| 79 |
gr.Textbox(label="生テキスト", show_copy_button=True, max_lines=5),
|
| 80 |
gr.Markdown(),
|
| 81 |
gr.JSON()
|
| 82 |
],
|
| 83 |
title="PDF to Dify Workflow",
|
| 84 |
+
description="PDFファイルを入力すると、Dify APIのワークフローによって処理された結果が表示されます。",
|
| 85 |
article="""
|
| 86 |
+
© 2024 @tregu0458. All rights reserved.
|
| 87 |
+
|
| 88 |
+
## 使用コンポーネント
|
| 89 |
+
- dify
|
| 90 |
+
- gradio
|
| 91 |
+
- langchain_community.document_loaders
|
| 92 |
+
|
| 93 |
+
## 今回のworkflowの仕様
|
| 94 |
+
### 入力
|
| 95 |
+
- url
|
| 96 |
+
- knowledge
|
| 97 |
+
### 出力
|
| 98 |
+
- result
|
| 99 |
+
- row_content
|
| 100 |
+
- url
|
| 101 |
+
### LLM
|
| 102 |
+
- gemini-1.5-flash
|
| 103 |
+
```
|
| 104 |
+
PDFファイルを入力として受け取り、Dify APIのワークフローを使用してファイルを処理し、結果を返す関数。
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
message (dict): 入力メッセージ。以下のキーを含む辞書。
|
| 108 |
+
- 'files' (list): アップロードされたPDFファイルのリスト。
|
| 109 |
+
- 'text' (str): テキストメッセージ。
|
| 110 |
+
|
| 111 |
+
Yields:
|
| 112 |
+
tuple: 以下の要素を含むタプル。
|
| 113 |
+
- raw_text (str): PDFファイルから抽出された生テキスト。
|
| 114 |
+
- assistant_message (str): アシスタントからのメッセージ。
|
| 115 |
+
- outputs (dict): APIレスポンスのデー���。
|
| 116 |
+
|
| 117 |
+
Returns:
|
| 118 |
+
tuple: 以下の要素を含むタプル。
|
| 119 |
+
- status (str): 処理の状態。"error" または "" (空文字列)。
|
| 120 |
+
- error_message (str): エラーメッセージ (エラーが発生した場合)。
|
| 121 |
+
- data (dict): APIレスポンスのデータ。
|
| 122 |
+
|
| 123 |
+
Raises:
|
| 124 |
+
Exception: 処理中にエラーが発生した場合。
|
| 125 |
|
| 126 |
+
Notes:
|
| 127 |
+
- 関数は非同期的に実行され、処理の進行状況に応じて段階的に結果を返す。
|
| 128 |
+
- `yield` を使用して、処理の途中経過を表示しながら、最終的な結果を返す。
|
| 129 |
+
```
|
| 130 |
+
"""
|
| 131 |
+
)
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
iface.queue().launch()
|