ramysaidagieb commited on
Commit
f826667
·
verified ·
1 Parent(s): ac14883

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -1,21 +1,25 @@
 
1
  import dspy
2
  import gradio as gr
3
  import chromadb
4
  import fitz # PyMuPDF
5
- from sentence_transformers import SentenceTransformer
6
  import json
7
- import os
8
  from dspy import Example, MIPROv2, Evaluate, evaluate
 
 
9
 
10
- # احصل على التوكن من متغيرات البيئة
11
- HF_TOKEN = os.environ["HF_TOKEN"]
12
-
13
- # إعداد نموذج اللغة باستخدام Hugging Face Inference API
14
  dspy.settings.configure(
15
- lm=dspy.HFInference(model="mistralai/Mistral-7B-Instruct-v0.2", token=HF_TOKEN)
 
 
 
 
16
  )
17
 
18
- # إعداد قاعدة Chroma
19
  client = chromadb.PersistentClient(path="./chroma_db")
20
  col = client.get_or_create_collection(name="arabic_docs")
21
 
@@ -35,7 +39,7 @@ def process_pdf(pdf_bytes):
35
 
36
  # إدخال البيانات في Chroma
37
  def ingest(pdf_file):
38
- pdf_bytes = pdf_file # لأننا استخدمنا type='binary'
39
  texts = process_pdf(pdf_bytes)
40
  embeddings = embedder.encode(texts, show_progress_bar=True)
41
  for i, (chunk, emb) in enumerate(zip(texts, embeddings)):
@@ -72,7 +76,7 @@ def answer(question):
72
  out = model(question)
73
  return out.answer
74
 
75
- # تحميل مجموعة بيانات التدريب/التحقق
76
  def load_dataset(path):
77
  with open(path, "r", encoding="utf-8") as f:
78
  return [Example(**json.loads(l)).with_inputs("question") for l in f]
@@ -89,7 +93,7 @@ def optimize(train_file, val_file):
89
 
90
  # واجهة Gradio
91
  with gr.Blocks() as demo:
92
- gr.Markdown("## 🧠 نظام RAG عربي باستخدام DSPy + ChromaDB + Mistral")
93
 
94
  with gr.Tab("📥 تحميل وتخزين"):
95
  pdf_input = gr.File(label="ارفع ملف PDF", type="binary")
 
1
+ import os
2
  import dspy
3
  import gradio as gr
4
  import chromadb
5
  import fitz # PyMuPDF
 
6
  import json
7
+ from sentence_transformers import SentenceTransformer
8
  from dspy import Example, MIPROv2, Evaluate, evaluate
9
+ from litellm import completion # Ensure LiteLLM is installed
10
+ from dspy.lm import LiteLLM
11
 
12
+ # إعداد نموذج اللغة باستخدام LiteLLM + Hugging Face
13
+ HF_TOKEN = os.environ.get("HF_TOKEN")
 
 
14
  dspy.settings.configure(
15
+ lm=LiteLLM(
16
+ model="HuggingFaceH4/zephyr-7b-beta",
17
+ api_base="https://api-inference.huggingface.co/v1",
18
+ api_key=HF_TOKEN
19
+ )
20
  )
21
 
22
+ # إعداد قاعدة بيانات Chroma
23
  client = chromadb.PersistentClient(path="./chroma_db")
24
  col = client.get_or_create_collection(name="arabic_docs")
25
 
 
39
 
40
  # إدخال البيانات في Chroma
41
  def ingest(pdf_file):
42
+ pdf_bytes = pdf_file
43
  texts = process_pdf(pdf_bytes)
44
  embeddings = embedder.encode(texts, show_progress_bar=True)
45
  for i, (chunk, emb) in enumerate(zip(texts, embeddings)):
 
76
  out = model(question)
77
  return out.answer
78
 
79
+ # تحميل بيانات التدريب والتحقق
80
  def load_dataset(path):
81
  with open(path, "r", encoding="utf-8") as f:
82
  return [Example(**json.loads(l)).with_inputs("question") for l in f]
 
93
 
94
  # واجهة Gradio
95
  with gr.Blocks() as demo:
96
+ gr.Markdown("## 🧠 نظام RAG عربي باستخدام DSPy + ChromaDB + Hugging Face")
97
 
98
  with gr.Tab("📥 تحميل وتخزين"):
99
  pdf_input = gr.File(label="ارفع ملف PDF", type="binary")