fudii0921's picture
Update app.py
71393fc verified
import gradio as gr
import cohere
import os
import json
from dotenv import load_dotenv
#from qdrant_client import QdrantClient
#from qdrant_client.models import Filter, FieldCondition, MatchValue
#import json
load_dotenv(verbose=True)
#client = QdrantClient(url="http://localhost:6333")
def get_scores_for_users(userids):
try:
userids_list = userids.split(",") # Split user IDs into a list
result_messages = [] # Store results for each userid
for userid in userids_list:
userid = userid.strip() # Clean up whitespace around user IDs
# Build filter to query points for the specific userid
filter_condition = {
"must": [
{
"key": "userid",
"match": {"value": userid}
}
]
}
# Query Qdrant for points matching the userid
results = client.scroll(
collection_name="skill_scores",
limit=6, # Adjust based on your needs
with_payload=True # Include payloads for manual filtering
)
filtered_results = [
point.payload["score"] for point in results[0]
if point.payload.get("userid") == userid
]
#result_messages.append(f'{{"{userid}": {filtered_results}}}')
result_messages.append(f'{{"userid":"{userid}","score": {filtered_results}}}')
# Join result_messages into a single JSON-compatible string
json_data = "[" + ",".join(result_messages) + "]"
# Parse the JSON string into Python data
parsed_data = json.loads(json_data)
# Optional: Print or process parsed data
print(parsed_data)
# useridの取り出し
userid = parsed_data[0]["userid"]
print(f"User ID: {userid}")
# scoreの各項目を取り出し
scores = parsed_data[0]["score"]
print(f"Score: {scores}")
#return "\n".join(result_messages) # Return results for all user IDs
return userid,scores
except Exception as e:
return f"Error occurred while retrieving scores: {str(e)}"
def update_lang(lang):
if len(lang) == 0:
return gr.update(value="Pythonが得意。スクリプト作成やデータ分析、機械学習まで幅広く活用している。")
else:
return gr.update(value=lang)
def update_debug(debug):
if len(debug) == 0:
return gr.update(value="よく使うのはpdb(Python Debugger)やVS Codeのデバッガ機能。エラー箇所を細かくチェックするのが好き。")
else:
return gr.update(value=debug)
def update_recent_problem(recent_problem):
if len(recent_problem) == 0:
return gr.update(value="例えば、APIの連携でエラーが続発する問題があったとき、レスポンスをしっかりロギングしてパターンを分析、結果的にリクエストヘッダーを微調整して解決できた。")
else:
return gr.update(value=recent_problem)
def update_project_approach(project_approach):
if len(project_approach) == 0:
return gr.update(value="チーム内でブレインストーミングしてアイデアを出し合い、最良の解決策を選択するのが常套手段だと思っている。")
else:
return gr.update(value=project_approach)
def update_project_scale(project_scale):
if len(project_scale) == 0:
return gr.update(value="1人で取り組むものから、数十人規模の開発チームでクラウドシステム構築などをした経験もある。")
else:
return gr.update(value=project_scale)
def update_team_role (team_role):
if len(team_role ) == 0:
return gr.update(value="主にテクニカルリーダーとして、設計からレビューまでを担当。時には新人教育も。")
else:
return gr.update(value=team_role)
def update_learning_methods(learning_methods):
if len(learning_methods) == 0:
return gr.update(value="実際に手を動かして試すのが一番。Tutorialやドキュメントを読みながらプロトタイプを作成。")
else:
return gr.update(value=learning_methods)
def update_recent_learning(recent_learning):
if len(recent_learning) == 0:
return gr.update(value="最近は生成AIのモデルチューニング方法について学んだよ。これでさらに便利なプロジェクトが作れる。")
else:
return gr.update(value=recent_learning)
# スコアを抽出する関数
def post_scores(state,email):
if len(email) == 0:
return "検査結果がありません!"
else:
state['final_email'] = email
state['final_comment'] = """{
"技術的な理解": 9,
"問題解決能力": 8.5,
"実務経験": 8,
"学習意欲": 9,
"ソフトスキル": 8.5,
"合計スコア": 8.6
}"""
co = cohere.ClientV2(api_key=os.environ.get("COHERE_API_KEY"))
res = co.chat(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": f"これは{state['final_email']}さんのデータです。{state['final_comment']}を評価してください。生成された結果に基づいて{state['final_email']}さんに送るスカウト提案メールも作ってください。答えは、必ず、評価結果とメールの本文を日本語で返してください。",
}
],
)
data = res.message.content[0].text
return data
# スコアを抽出する関数
def extract_scores(state):
if len(state["final_score"]) == 0:
return "検査結果がありません!"
else:
co = cohere.ClientV2(api_key=os.environ.get("COHERE_API_KEY"))
res = co.chat(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": f"これは{state['final_email']}さんのデータです。{state['final_comment']}を評価してください。生成された結果に基づいて{state['final_email']}さんに送るスカウト提案メールも作ってください。答えは、必ず、評価結果とメールの本文を日本語で返してください。",
}
],
)
data = res.message.content[0].text
return data
# 簡易評価の関数
def evaluate_responses(state, email, programming, debugging, problem, problem_solving, project_experience, role, learning, learnt_skills):
state["final_email"] = email
if (email != "" and programming != "" and debugging != "" and problem != "" and problem_solving != "" and project_experience != "" and role != "" and learning != "" and learnt_skills != ""):
# レコード作成
records = f"""
✨プログラミング言語について: {programming}
✨デバッグ方法について: {debugging}
✨最近解決した技術的な課題: {problem}
✨プロジェクト問題解決方法について: {problem_solving}
✨プロジェクト経験について: {project_experience}
✨チームでの役割について: {role}
✨新しい技術への学習意欲について: {learning}
✨最近学んだ技術や知識について: {learnt_skills}
"""
co = cohere.ClientV2(api_key=os.environ.get("COHERE_API_KEY"))
res = co.chat(
model="command-a-03-2025",
messages=[
{
"role": "user",
"content": f"""
{records}を「基準」に基づいて各項目の合計スコアを10段階で割り振って評価してください。答えは、必ず、「形式」で合計スコアのみを返してください。
「基準」=価基準とポイント配分
技術的な理解 (30%): 例えばプログラミング言語の知識やデバッグ能力を考慮。コード品質や問題解決能力が評価対象。
問題解決能力 (25%): 技術的課題への対応スキルやアプローチ方法をチェック。現実的かつ効率的な解決策を出せるかどうか。
実務経験 (20%): 過去のプロジェクト経験の内容や規模。実際の成果や役割が評価される。
学習意欲 (15%): 新しい技術をどれだけ積極的に学び、適応しているか。最近学んだ知識がアピールポイント!
ソフトスキル (10%): チームワークやコミュニケーション能力。エンジニアの柔軟性や協力姿勢を評価。
「形式」= {{
"技術的な理解": 9,
"問題解決能力": 8.5,
"実務経験": 8,
"学習意欲": 9.5,
"ソフトスキル": 8.5,
"合計スコア": 8.63,
"スキルレベル": "Expert",
"説明": "技術力や問題解決能力、学習意欲が高くバランスの良いスキルセットを持っている。"
}}
""",
}
],
)
cleaned_string = res.message.content[0].text.replace("```json", "").replace("```", "").strip()
data = json.loads(cleaned_string)
#data = json.loads(res.message.content[0].text)
comments = "\n".join([f"{key}: {value}" for key, value in data.items()])
scores = [f"{key}: {value}" for key, value in data.items()]
print("db-scores:",scores)
state["final_comment"] = comments
state["final_score"] = scores
return records, comments
else:
return "入力が正しくありません。","None"
# Gradio Blocksの構築
with gr.Blocks(css="footer {visibility: hidden;} #header {display: flex; justify-content: space-between; align-items: center; font-size: 24px; font-weight: bold;} #logo {width: 50px; height: 50px;}", theme=gr.themes.Glass(), title="エンジニア・スキル評価") as inquery:
state = gr.State({
"final_email": "",
"final_comment": "",
"final_score": [],
})
gr.HTML('<div id="header"><span>🧑‍🏫👩‍🏫 エンジニア・スキル評価</span><img id="logo" src="https://www.ryhintl.com/images/ryhlogo/ryhlogo.png" width="64" height="64" alt="Logo"></div>')
gr.Markdown("エンジニア・スキルを簡易評価するアプリです!")
with gr.Tab("エンジニア・スキル評価質問フォーム"):
# 入力フィールド
with gr.Row():
email = gr.Textbox(label="電子メールを入力してください。", info="your mail address")
with gr.Row():
lang = gr.Textbox(label="普段使用しているプログラミング言語は何ですか?、尚、経験年数を教えてください。",
info="Pythonが得意。スクリプト作成やデータ分析、機械学習まで幅広く活用している。", value="")
lang.submit(fn=update_lang,inputs=[lang],outputs=[lang])
with gr.Row():
debug_tools = gr.Textbox(label="コードのデバッグ方法やツールについて教えてください。",
info="よく使うのはpdb(Python Debugger)やVS Codeのデバッガ機能。エラー箇所を細かくチェックするのが好き。")
debug_tools.submit(fn=update_debug,inputs=[debug_tools],outputs=[debug_tools])
with gr.Row():
recent_problem = gr.Textbox(label="最近解決した技術的な課題を教えてください。どのようにアプローチしましたか?",
info="例えば、APIの連携でエラーが続発する問題があったとき、レスポンスをしっかりロギングしてパターンを分析、結果的にリクエストヘッダーを微調整して解決できた。")
recent_problem.submit(fn=update_recent_problem,inputs=[recent_problem],outputs=[recent_problem])
with gr.Row():
project_approach = gr.Textbox(label="複雑なプロジェクトで課題が発生した場合、どう対処しますか?",
info="チーム内でブレインストーミングしてアイデアを出し合い、最良の解決策を選択するのが常套手段だと思っている。")
project_approach.submit(fn=update_project_approach,inputs=[project_approach],outputs=[project_approach])
with gr.Row():
project_scale = gr.Textbox(label="過去に関わったプロジェクトの規模や内容を教えてください。",
info="1人で取り組むものから、数十人規模の開発チームでクラウドシステム構築などをした経験もある。")
project_scale.submit(fn=update_project_scale,inputs=[project_scale],outputs=[project_scale])
with gr.Row():
team_role = gr.Textbox(label="チームでどのような役割を果たしましたか?",
info="主にテクニカルリーダーとして、設計からレビューまでを担当。時には新人教育も。")
team_role.submit(fn=update_team_role,inputs=[team_role],outputs=[team_role])
with gr.Row():
learning_methods = gr.Textbox(label="新しい技術を学ぶ際にどのような方法を取っていますか?",
info="実際に手を動かして試すのが一番。Tutorialやドキュメントを読みながらプロトタイプを作成。")
learning_methods.submit(fn=update_learning_methods,inputs=[learning_methods],outputs=[learning_methods])
with gr.Row():
recent_learning = gr.Textbox(label="最近学んだ技術や知識は何ですか?",
info="最近は生成AIのモデルチューニング方法について学んだよ。これでさらに便利なプロジェクトが作れる。")
recent_learning.submit(fn=update_recent_learning,inputs=[recent_learning],outputs=[recent_learning])
# ボタンと出力フィールド
with gr.Row():
submit_btn = gr.Button("スキル評価")
with gr.Row():
result = gr.Textbox(label="結果", show_copy_button=True)
score = gr.Textbox(label="スコア")
submit_btn.click(fn=evaluate_responses,
inputs=[state, email, lang, debug_tools, recent_problem, project_approach,
project_scale, team_role, learning_methods, recent_learning],
outputs=[result, score])
with gr.Tab("リアルタイム・アセスメント"):
gr.Markdown("# エンジニア・スキルの簡易評価")
gr.Markdown("質問フォームから各項目のスコアを抽出して評価します!")
with gr.Row():
display_button = gr.Button("簡易評価")
with gr.Row():
output = gr.Textbox(label="簡易評価", show_copy_button=True)
# ボタンに関数を接続
display_button.click(fn=extract_scores, inputs=[state], outputs=output)
with gr.Tab("ポスト・アセスメント"):
gr.Markdown("# エンジニア・スキルの簡易評価")
gr.Markdown("質問フォームから各項目のスコアを抽出して評価します!")
with gr.Row():
input = gr.Textbox(label="電子メール",value="richardhuh0629@gmail.com")
output = gr.Textbox(label="簡易評価", show_copy_button=True)
with gr.Row():
analyze_button = gr.Button("簡易評価")
# ボタンに関数を接続
analyze_button.click(fn=post_scores, inputs=[state,input], outputs=output)
inquery.launch()