meshsl commited on
Commit
3b255c5
·
verified ·
1 Parent(s): 13790dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -1,31 +1,30 @@
1
- from fastapi import FastAPI, Query
2
- from langchain_community.vectorstores import Chroma
3
- from langchain_community.embeddings import HuggingFaceEmbeddings
4
- from together import Together
5
  import os
 
 
6
 
7
- embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
8
- vectordb = Chroma(persist_directory="chroma_db", embedding_function=embedding)
 
 
 
 
9
 
10
- TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY", "")
11
- client = Together(api_key=TOGETHER_API_KEY)
 
12
 
13
- def call_llama(prompt: str):
14
- response = client.chat.completions.create(
15
- model="meta-llama/Llama-3-8b-chat-hf",
16
- messages=[
17
- {"role": "system", "content": "You are a helpful assistant."},
18
- {"role": "user", "content": prompt}
19
- ]
20
- )
21
- return response.choices[0].message.content
22
 
23
- app = FastAPI()
 
 
 
 
 
 
 
24
 
25
- @app.get("/ask")
26
- async def ask(q: str = Query(..., description="Your question")):
27
- docs = vectordb.similarity_search(q, k=3)
28
- context = "\n".join([doc.page_content for doc in docs])
29
- final_prompt = f"Use the context below to answer the question.\n\nContext:\n{context}\n\nQuestion: {q}"
30
- answer = call_llama(final_prompt)
31
- return {"answer": answer}
 
 
 
 
 
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()