Spaces:
No application file
No application file
Upload app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,13 @@ from sentence_transformers import SentenceTransformer, util
|
|
| 8 |
# OpenAIキー
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
-
# ZIP展開
|
| 12 |
if not os.path.exists("articles"):
|
| 13 |
with zipfile.ZipFile("articles.zip", "r") as zip_ref:
|
| 14 |
zip_ref.extractall("articles")
|
| 15 |
|
| 16 |
-
# CSV読み込み(
|
| 17 |
-
documents = []
|
| 18 |
-
file_map = {}
|
| 19 |
-
|
| 20 |
with open("articles/articles.csv", "r", encoding="cp932") as f:
|
| 21 |
reader = csv.DictReader(f)
|
| 22 |
for row in reader:
|
|
@@ -24,7 +22,7 @@ with open("articles/articles.csv", "r", encoding="cp932") as f:
|
|
| 24 |
documents.append(content)
|
| 25 |
file_map[content] = {"title": row["title"], "url": row["url"]}
|
| 26 |
|
| 27 |
-
# 埋め込みモデル読み込み
|
| 28 |
model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 29 |
document_embeddings = model.encode(documents, convert_to_tensor=True)
|
| 30 |
|
|
@@ -34,16 +32,17 @@ def generate_answer(query):
|
|
| 34 |
hits = util.semantic_search(query_embedding, document_embeddings, top_k=2)[0]
|
| 35 |
|
| 36 |
sources = []
|
|
|
|
| 37 |
for hit in hits:
|
| 38 |
content = documents[hit["corpus_id"]]
|
| 39 |
meta = file_map[content]
|
| 40 |
sources.append(meta["url"])
|
|
|
|
| 41 |
|
| 42 |
-
context = "\n\n".join(
|
| 43 |
prompt = (
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
-
f"質問: {query}\n"
|
| 47 |
f"---\n{context}\n---"
|
| 48 |
)
|
| 49 |
|
|
@@ -54,28 +53,39 @@ def generate_answer(query):
|
|
| 54 |
temperature=0.6,
|
| 55 |
)
|
| 56 |
answer = response.choices[0].message.content.strip()
|
| 57 |
-
|
| 58 |
-
return f"{answer}\n\nもっと詳しく知りたい方は、以下もご覧ください:\n{
|
| 59 |
except Exception as e:
|
| 60 |
-
return f"エラーが発生しました:
|
| 61 |
|
| 62 |
-
# UI
|
| 63 |
-
with gr.Blocks(
|
| 64 |
gr.Markdown("## 面接で不安なことを入力して下さい")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
-
with gr.Column(
|
| 68 |
question = gr.Textbox(
|
| 69 |
label="質問",
|
| 70 |
placeholder="例:面接で緊張してしまいます。どうすればよいですか?",
|
| 71 |
lines=2,
|
| 72 |
)
|
| 73 |
-
submit_btn = gr.Button("転職面接ナビに回答してもらう",
|
| 74 |
clear_btn = gr.Button("リセット")
|
| 75 |
-
with gr.Column(
|
| 76 |
answer = gr.Textbox(label="回答", lines=10)
|
| 77 |
|
| 78 |
submit_btn.click(fn=generate_answer, inputs=question, outputs=answer)
|
| 79 |
clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[question, answer])
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
demo.launch()
|
|
|
|
| 8 |
# OpenAIキー
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
+
# ZIPを展開
|
| 12 |
if not os.path.exists("articles"):
|
| 13 |
with zipfile.ZipFile("articles.zip", "r") as zip_ref:
|
| 14 |
zip_ref.extractall("articles")
|
| 15 |
|
| 16 |
+
# CSVから記事とURLを読み込み(cp932対応)
|
| 17 |
+
documents, file_map = [], {}
|
|
|
|
|
|
|
| 18 |
with open("articles/articles.csv", "r", encoding="cp932") as f:
|
| 19 |
reader = csv.DictReader(f)
|
| 20 |
for row in reader:
|
|
|
|
| 22 |
documents.append(content)
|
| 23 |
file_map[content] = {"title": row["title"], "url": row["url"]}
|
| 24 |
|
| 25 |
+
# 埋め込みモデルの読み込み
|
| 26 |
model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
| 27 |
document_embeddings = model.encode(documents, convert_to_tensor=True)
|
| 28 |
|
|
|
|
| 32 |
hits = util.semantic_search(query_embedding, document_embeddings, top_k=2)[0]
|
| 33 |
|
| 34 |
sources = []
|
| 35 |
+
context_parts = []
|
| 36 |
for hit in hits:
|
| 37 |
content = documents[hit["corpus_id"]]
|
| 38 |
meta = file_map[content]
|
| 39 |
sources.append(meta["url"])
|
| 40 |
+
context_parts.append(content[:300])
|
| 41 |
|
| 42 |
+
context = "\n\n".join(context_parts)
|
| 43 |
prompt = (
|
| 44 |
+
f"以下は面接に関する記事の一部です。共感を込めて丁寧に自然な日本語で質問に答えてください。\n\n"
|
| 45 |
+
f"質問:{query}\n"
|
|
|
|
| 46 |
f"---\n{context}\n---"
|
| 47 |
)
|
| 48 |
|
|
|
|
| 53 |
temperature=0.6,
|
| 54 |
)
|
| 55 |
answer = response.choices[0].message.content.strip()
|
| 56 |
+
reference_links = "\n".join(f"{url}" for url in sources)
|
| 57 |
+
return f"{answer}\n\nもっと詳しく知りたい方は、以下もご覧ください:\n{reference_links}"
|
| 58 |
except Exception as e:
|
| 59 |
+
return f"エラーが発生しました:\n{str(e)}"
|
| 60 |
|
| 61 |
+
# Gradio UI
|
| 62 |
+
with gr.Blocks(title="面接で不安なことを入力して下さい") as demo:
|
| 63 |
gr.Markdown("## 面接で不安なことを入力して下さい")
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
question = gr.Textbox(
|
| 68 |
label="質問",
|
| 69 |
placeholder="例:面接で緊張してしまいます。どうすればよいですか?",
|
| 70 |
lines=2,
|
| 71 |
)
|
| 72 |
+
submit_btn = gr.Button("転職面接ナビに回答してもらう", elem_id="submit-btn")
|
| 73 |
clear_btn = gr.Button("リセット")
|
| 74 |
+
with gr.Column():
|
| 75 |
answer = gr.Textbox(label="回答", lines=10)
|
| 76 |
|
| 77 |
submit_btn.click(fn=generate_answer, inputs=question, outputs=answer)
|
| 78 |
clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[question, answer])
|
| 79 |
|
| 80 |
+
# ボタン色カスタムJS
|
| 81 |
+
demo.load(None, None, None, js="""
|
| 82 |
+
() => {
|
| 83 |
+
const btn = document.querySelector("#submit-btn");
|
| 84 |
+
if (btn) {
|
| 85 |
+
btn.style.backgroundColor = "#f97316"; // tailwind orange-500
|
| 86 |
+
btn.style.color = "white";
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
""")
|
| 90 |
+
|
| 91 |
demo.launch()
|