meshsl commited on
Commit
9548472
·
verified ·
1 Parent(s): 1e8db85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -23
app.py CHANGED
@@ -1,30 +1,31 @@
1
- import os
2
- import gradio as gr
3
- from rag_system import RAGSystem # تأكد أن الملف اسمه rag_system.py وموجود في نفس المجلد
4
 
5
- # --- تحميل النظام ---
6
- rag = RAGSystem(
7
- vector_db_path="vector_db", # تأكد أن قاعدة البيانات مضغوطة ومرفوعة ضمن files
8
- model_name="meta-llama/Llama-3-8b-chat-hf",
9
- embedding_model_name="sentence-transformers/all-mpnet-base-v2"
 
 
 
 
 
 
 
 
10
  )
11
 
 
 
12
  rag.load_vectorstore()
13
  rag.load_llm()
14
  rag.get_prompt_template()
15
 
16
- # --- دالة الاستجابة ---
17
- def ask_question_interface(question):
18
- return rag.ask_question(question)
19
-
20
- # --- واجهة Gradio ---
21
- demo = gr.Interface(
22
- fn=ask_question_interface,
23
- inputs=gr.Textbox(label="Ask a question about the ECC Guide"),
24
- outputs=gr.Textbox(label="Answer"),
25
- title="ECC Cybersecurity Assistant",
26
- description="Ask questions based on the Essential Cybersecurity Controls (ECC) Implementation Guide issued by the NCA (Saudi Arabia).",
27
- )
28
-
29
- if __name__ == "__main__":
30
- demo.launch()
 
1
+ # app.py
 
 
2
 
3
+ from fastapi import FastAPI, HTTPException
4
+ from pydantic import BaseModel
5
+ from rag_system import RAGSystem
6
+
7
+ # نموذج بيانات الطلب
8
+ class QuestionRequest(BaseModel):
9
+ question: str
10
+
11
+ # إنشاء التطبيق
12
+ app = FastAPI(
13
+ title="ECC Compliance Assistant API",
14
+ description="Ask questions based on the ECC Cybersecurity Guide using RAG",
15
+ version="1.0.0"
16
  )
17
 
18
+ # تحميل النظام عند بدء التشغيل
19
+ rag = RAGSystem(vector_db_path="vector_db")
20
  rag.load_vectorstore()
21
  rag.load_llm()
22
  rag.get_prompt_template()
23
 
24
+ # نقطة النهاية
25
+ @app.post("/ask")
26
+ async def ask_question(data: QuestionRequest):
27
+ try:
28
+ answer = rag.ask_question(data.question)
29
+ return {"question": data.question, "answer": answer}
30
+ except Exception as e:
31
+ raise HTTPException(status_code=500, detail=str(e))