Spaces:
Runtime error

File size: 1,694 Bytes
2255901
 
 
7b06538
2255901
 
e26ace0
2255901
 
 
 
 
 
 
 
b0cbcd7
2255901
 
 
 
 
 
d9bbb58
2255901
 
 
 
 
 
 
 
 
 
 
7b06538
 
 
 
 
 
 
 
 
 
 
 
 
2255901
 
7b06538
2255901
7b06538
 
2255901
7b06538
 
2255901
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()