Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# 環境変数からDIFY_BASE_URLとDIFY_API_KEY_MYWORKFLOWを取得
|
| 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 |
+
yield raw_text,"loading...",{}
|
| 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()
|
| 46 |
+
if chunk_data.startswith("data:"):
|
| 47 |
+
json_data = chunk_data[6:] # "data: "を取り除く
|
| 48 |
+
if json_data:
|
| 49 |
+
result = json.loads(json_data)
|
| 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.File(label="PDFファイルをアップロード", file_types=[".pdf"])],
|
| 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 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
iface.queue().launch()
|