mook84816 commited on
Commit
f6863e5
·
1 Parent(s): 3c374af

Switch to Japanese summarization app

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -2,24 +2,43 @@ import os
2
  import gradio as gr
3
  import requests
4
 
5
- API_URL = "https://api-inference.huggingface.co/models/ClovaAI/medium-small-japanese-summarization"
6
- headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
 
 
7
 
8
- print(f"APIキー(先頭10文字のみ表示): {os.getenv('HUGGINGFACE_API_KEY')[:10]}") # ←ここを追加!
9
 
10
  def summarize(text):
11
- response = requests.post(API_URL, headers=headers, json={"inputs": text})
12
- if response.status_code != 200:
13
- print(f"エラー詳細: {response.status_code} - {response.text}")
14
- return "要約に失敗しました。"
15
- summary = response.json()
16
- return summary[0]["summary_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
18
  iface = gr.Interface(
19
  fn=summarize,
20
- inputs=gr.Textbox(label="ニュース記事を入力"),
21
  outputs=gr.Textbox(label="要約結果"),
22
- title="日本語ニュース要約"
 
23
  )
24
 
25
- iface.launch()
 
 
2
  import gradio as gr
3
  import requests
4
 
5
+ # Hugging Face SpacesのSecretsで設定したAPIキーを取得
6
+ API_KEY = os.environ.get("HUGGINGFACE_API_KEY")
7
+ if not API_KEY:
8
+ raise ValueError("APIキーが設定されていません。SpacesのSettings > SecretsでHUGGINGFACE_API_KEYを追加してください。")
9
 
10
+ MODEL_URL = "https://api-inference.huggingface.co/models/sonoisa/t5-base-japanese-news-summary"
11
 
12
  def summarize(text):
13
+ print("要約開始:", text[:30], "...")
14
+ headers = {
15
+ "Authorization": f"Bearer {API_KEY}"
16
+ }
17
+ payload = {
18
+ "inputs": text
19
+ }
20
+ try:
21
+ response = requests.post(MODEL_URL, headers=headers, json=payload)
22
+ print("APIレスポンスステータス:", response.status_code)
23
+ print("APIレスポンス内容:", response.text)
24
+ response.raise_for_status()
25
+ data = response.json()
26
+ if isinstance(data, list) and "summary_text" in data[0]:
27
+ return data[0]["summary_text"]
28
+ else:
29
+ return "要約に失敗しました。レスポンス: " + str(data)
30
+ except Exception as e:
31
+ print("要約処理中に例外:", e)
32
+ return f"要約失敗: {e}"
33
 
34
+ # Gradioインターフェース
35
  iface = gr.Interface(
36
  fn=summarize,
37
+ inputs=gr.Textbox(label="要約する日本語ニュース記事", placeholder="ここにニュース記事を入力してください...", lines=6),
38
  outputs=gr.Textbox(label="要約結果"),
39
+ title="日本語ニュース要約アプリ",
40
+ description="Hugging Faceの日本語要約モデルを使用して、入力した日本語ニュース記事を短く要約します。"
41
  )
42
 
43
+ if __name__ == "__main__":
44
+ iface.launch()