Corin1998 commited on
Commit
be1ef6d
·
verified ·
1 Parent(s): 16e3595

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from huggingface_hub import InferenceClient
4
+ from duckduckgo_search import DDGS
5
+ import json
6
+
7
+ # --- 1. 初期設定 ---
8
+ # HFのアクセストークン(Settings > Access Tokensで作成)を、
9
+ # Spaces の Settings > Variables and Secrets に 「HR_TOKEN」として保存してください。
10
+ import os
11
+ token = os.getenv("HF_TOKEN")
12
+ client = TnferenceClient("meta-llama/Llama-3.1-8B-Instruct", token=token)
13
+
14
+ st.set_pae_cofig(page_title="企業情報リサーチAI", layout="wide")
15
+ st.title("🏢 企業情報一覧生成プロトタイプ")
16
+
17
+ # --- 2. 検索ロジック ---
18
+ def get_web_info(query):
19
+ """DuckDuckGoを使用してWeb検索結果のようやくを取得"""
20
+ with DDGS() as ddgs:
21
+ results = [r for r in ddgs.text(query, max_results=5)]
22
+ return "\n".join([f"Source:{r['href']}\nContent: {r['body']}" for r in results])
23
+
24
+ # --- 3. UI部分 ---
25
+ theme = st.text_input("リサーチしたいテーマを入力してください。", placeholder="例: 日本国内の生成AIスタートアップ")
26
+
27
+ if st.button("情報を収集してリスト化"):
28
+ if not token:
29
+ st.error("HF_TOKENが設定されていません。SettingsからSecretを設定してください。")
30
+ else:
31
+ with st.spinner("Webから情報を検索中..."):
32
+ # Web検索実行
33
+ raw_data = get_web_info(theme)
34
+
35
+ # AIへのプロンプトreturn"区生
36
+ prompt = f"""
37
+ 以下のWeb検索結果を元に、テーマ「{theme}」に関連する企業のリストを作成してください。
38
+ 出力は必ず以下のキーを持つJSON形式のリストにしてください:
39
+ "companies": [{"name":"企業名", "description":"企業説明", "website":"企業のウェブサイトURL"}]
40
+
41
+ 【検索結果】:
42
+ {raw_data}
43
+ """
44
+
45
+ try:
46
+ # AIによる構造化
47
+ response = clent.chat_completion(
48
+ messages=[{"role": "user", "content": prompt}],
49
+ max_tokens=1500,
50
+ response_format={"type": "json_object"}
51
+ )
52
+
53
+ # 結果の解析と表示
54
+ res_content = response.choices[0].message.content
55
+ data = json.loads(res_content)
56
+ df = pd.DataFrame(data["companies"])
57
+
58
+ st.success("リストの生成が完了しました!")
59
+ st.dataframe(df, use_container_windth=True)
60
+
61
+ #CSVダウンロード機能
62
+ csv = df.to_csv(index=False).encode('utf-8')
63
+ st.download_button("CSVとしてダウンロード", csv, "company_list.csv", "compnay_list.csv")
64
+
65
+ except Exception as e:
66
+ st.error(f"エラーが発生しました: {e}")
67
+ st.write("AIの応答":, res_content)