Spaces:
Build error
Build error
AI Assistant
feat: add support for Gemini API and implement state management for quiz turns and answers
b85377a Download app/routers/webhook.py from amirelrefai/securityChatbot: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/spaces/amirelrefai/securityChatbot/resolve/main/app/routers/webhook.py
- Command line
-
hf download hf://spaces/amirelrefai/securityChatbot/app/routers/webhook.py
-
curl -L -o webhook.py https://huggingface.co/spaces/amirelrefai/securityChatbot/resolve/main/app/routers/webhook.py
3.65 kB
| from fastapi import APIRouter | |
| from app.models.schemas import MessagePayload | |
| from app.services.state_manager import * | |
| from app.services.llm_service import * | |
| router = APIRouter() | |
| async def handle_webhook(payload: MessagePayload): | |
| try: | |
| user_id = payload.user_id | |
| message = payload.message.strip() | |
| current_state = get_state(user_id) | |
| if message == "/train": | |
| set_state(user_id, "CHOOSING_MODE") | |
| return {"response": "يا هندسة اختار عايز إيه:\n1- محاكاة اختراق (Simulation)\n2- اختبار معلومات (Quiz)"} | |
| if current_state == "CHOOSING_MODE": | |
| if message == "1": | |
| set_state(user_id, "CHOOSING_SCENARIO") | |
| return {"response": "اختار السيناريو:\n1- بنك\n2- شحن\n3- دعم فني\n4- واتساب\n5- مسابقة"} | |
| elif message == "2": | |
| set_state(user_id, "QUIZ_ACTIVE") | |
| quiz_data = get_quiz_llm() | |
| lines = quiz_data.split("\n") | |
| question_text = "\n".join([line for line in lines if "Correct Answer" not in line and "Explain" not in line]) | |
| correct_answer = [line for line in lines if "Correct Answer" in line][0].split(":")[1].strip() | |
| explanation = [line for line in lines if "Explain" in line][0].split(":")[1].strip() | |
| set_quiz_answer(user_id, f"{correct_answer}|{explanation}") | |
| return {"response": f"يلا بينا يا بطل، ده السؤال:\n\n{question_text}"} | |
| if current_state == "QUIZ_ACTIVE": | |
| stored_data = get_quiz_answer(user_id).split("|") | |
| correct = stored_data[0].lower() | |
| explanation = stored_data[1] | |
| user_ans = message.lower().replace(")", "").strip() | |
| set_state(user_id, "NORMAL") | |
| if user_ans == correct: | |
| return {"response": f"الله ينور! إجابة صح.\n\nالمعلومة: {explanation}"} | |
| else: | |
| return {"response": f"للأسف غلط.. الإجابة الصح كانت ({correct.upper()}).\n\nالمعلومة: {explanation}"} | |
| if current_state == "CHOOSING_SCENARIO": | |
| if message in SCENARIOS: | |
| set_scenario(user_id, message) | |
| set_state(user_id, "CHOOSING_LEVEL") | |
| return {"response": "اختار المستوى:\n1- سهل\n2- متوسط\n3- صعب"} | |
| return {"response": "اختار رقم من 1 لـ 5 يا غالي."} | |
| if current_state == "CHOOSING_LEVEL": | |
| if message in DIFFICULTY_LEVELS: | |
| set_level(user_id, message) | |
| set_state(user_id, "SIMULATION") | |
| reset_turns(user_id) | |
| return {"response": attacker_llm(user_id, "ابدأ المحادثة")} | |
| return {"response": "اختار ليفل من 1 لـ 3."} | |
| if current_state == "SIMULATION": | |
| if evaluator_llm(user_id, message) == "FAILED": | |
| set_state(user_id, "NORMAL") | |
| return {"response": "وقعت في الفخ! البنك مش بيطلب OTP أبداً."} | |
| increment_turn(user_id) | |
| if get_turn(user_id) >= 4: | |
| set_state(user_id, "NORMAL") | |
| return {"response": "عاش! قدرت تصد المهاجم بنجاح."} | |
| return {"response": attacker_llm(user_id, message)} | |
| return {"response": normal_chat_llm(message)} | |
| except Exception as e: | |
| return {"response": f"حصل عطل فني: {str(e)}"} |