Spaces:
Runtime error
Runtime error
Download app.py from sora19ai/Dify_Slide: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/sora19ai/Dify_Slide/resolve/main/app.py
- Command line
-
hf download hf://spaces/sora19ai/Dify_Slide/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sora19ai/Dify_Slide/resolve/main/app.py
1.69 kB
| import fitz # PyMuPDF | |
| import requests | |
| import gradio as gr | |
| import pandas as pd | |
| # Dify APIの設定 | |
| DIFY_API_KEY = "app-qN3gthdzDaQFaV4Ek0JxZotq" | |
| def run_dify_workflow(text): | |
| headers = { | |
| "Content-Type": "application/json", | |
| "Authorization": f"Bearer {DIFY_API_KEY}" | |
| } | |
| data = { | |
| "inputs": { | |
| "input":text | |
| }, | |
| "user": "abc-123" # ユーザー識別子 | |
| } | |
| response = requests.post(f"https://api.dify.ai/v1/workflows/run", headers=headers, json=data) | |
| try: | |
| response.raise_for_status() | |
| return response.json()["data"]["outputs"]["text"] | |
| except requests.exceptions.HTTPError as err: | |
| return f"Error: {str(err)}, Response: {response.text}" | |
| # PDFからテキスト抽出 | |
| def extract_text_from_pdf(pdf_path): | |
| doc = fitz.open(pdf_path) | |
| text = "" | |
| for page in doc: | |
| text += page.get_text() | |
| return text | |
| def extract_data_from_file(file_path): | |
| if file_path.endswith('.csv'): | |
| df = pd.read_csv(file_path) | |
| elif file_path.endswith(('.xls', '.xlsx')): | |
| df = pd.read_excel(file_path) | |
| else: | |
| raise ValueError("サポートされていないファイル形式です。") | |
| return df.to_csv(index=False) | |
| def process_file(file): | |
| data = extract_data_from_file(file.name) | |
| result = run_dify_workflow(data) | |
| return result | |
| # Gradioインターフェースの作成 | |
| iface = gr.Interface( | |
| fn=process_file, | |
| inputs=gr.File(file_types=[".csv", ".xls", ".xlsx"]), | |
| outputs="text", | |
| title="CSV/Excel to Dify Workflow", | |
| description="CSVまたはExcelファイルをアップロードしてDify APIで処理します。" | |
| ) | |
| iface.launch() |