diginoron commited on
Commit
03bc35b
·
verified ·
1 Parent(s): ae21b5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -8,15 +8,18 @@ from pinecone import Pinecone
8
  from sentence_transformers import SentenceTransformer
9
  from transformers import AutoTokenizer, AutoModelForCausalLM
10
 
11
- # 🔐 گرفتن کلیدها از Environment Variables (Secrets در Hugging Face)
12
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
13
  PINECONE_INDEX_NAME = os.environ.get("INDEX_NAME", "tiyam-chat")
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
15
 
16
- # 🔹 بارگذاری مدل embedding
17
- embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
 
 
 
18
 
19
- # 🔹 بارگذاری داده‌ها (اختیاری برای تست لوکال)
20
  with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
21
  data = json.load(f)
22
 
@@ -24,13 +27,13 @@ with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
24
  pc = Pinecone(api_key=PINECONE_API_KEY)
25
  index = pc.Index(PINECONE_INDEX_NAME)
26
 
27
- # 🔹 بارگذاری مدل GEMMA
28
  tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
29
  model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
30
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
31
  model = model.to(device)
32
 
33
- # 🔹 گرفتن پاسخ از Pinecone
34
  def retrieve_answer(query, threshold=0.65, top_k=3):
35
  query_embedding = embedding_model.encode([query])[0]
36
  result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
 
8
  from sentence_transformers import SentenceTransformer
9
  from transformers import AutoTokenizer, AutoModelForCausalLM
10
 
11
+ # 🔐 گرفتن سکرت‌ها از محیط
12
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
13
  PINECONE_INDEX_NAME = os.environ.get("INDEX_NAME", "tiyam-chat")
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
15
 
16
+ # 🔹 بارگذاری مدل embedding (با توکن)
17
+ embedding_model = SentenceTransformer(
18
+ 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2',
19
+ use_auth_token=HF_TOKEN
20
+ )
21
 
22
+ # 🔹 بارگذاری داده‌ها (اختیاری – فقط برای نمایش اولیه یا تست)
23
  with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
24
  data = json.load(f)
25
 
 
27
  pc = Pinecone(api_key=PINECONE_API_KEY)
28
  index = pc.Index(PINECONE_INDEX_NAME)
29
 
30
+ # 🔹 بارگذاری مدل GEMMA برای بازنویسی
31
  tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
32
  model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", token=HF_TOKEN)
33
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
34
  model = model.to(device)
35
 
36
+ # 🔹 بازیابی پاسخ اولیه از Pinecone
37
  def retrieve_answer(query, threshold=0.65, top_k=3):
38
  query_embedding = embedding_model.encode([query])[0]
39
  result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)