diginoron commited on
Commit
10ed581
·
verified ·
1 Parent(s): 9d627b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -1,11 +1,11 @@
1
- from sentence_transformers import SentenceTransformer
2
- from pinecone import Pinecone
3
  import json
4
  import gradio as gr
5
- import os
6
  import openai
 
 
7
 
8
- # استفاده از متغیر محیطی 'openai' برای کلید API
9
  openai.api_key = os.getenv("openai")
10
 
11
  # بارگذاری مدل embedding
@@ -15,9 +15,20 @@ model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
15
  with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
16
  data = json.load(f)
17
 
 
 
 
 
 
18
  # اتصال به Pinecone
19
- pc = Pinecone(api_key="PINECONE_API_KEY") # کلید خودت رو اینجا بگذار
20
- index = pc.Index("tiyam-chat")
 
 
 
 
 
 
21
 
22
  def retrieve_answer(query, threshold=0.65, top_k=1):
23
  query_embedding = model.encode([query])[0]
@@ -49,7 +60,7 @@ def generate_human_response(context_text):
49
  max_tokens=100,
50
  )
51
  return response['choices'][0]['message']['content'].strip()
52
- except Exception as e:
53
  return "خطا در پردازش درخواست."
54
 
55
  def chat_interface(question):
 
1
+ import os
 
2
  import json
3
  import gradio as gr
 
4
  import openai
5
+ from sentence_transformers import SentenceTransformer
6
+ import pinecone
7
 
8
+ # بارگذاری کلید OpenAI از متغیر محیطی
9
  openai.api_key = os.getenv("openai")
10
 
11
  # بارگذاری مدل embedding
 
15
  with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
16
  data = json.load(f)
17
 
18
+ # دریافت مقادیر متغیرهای محیطی مربوط به Pinecone
19
+ PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
20
+ PINECONE_ENVIRONMENT = os.getenv("PINECONE_ENVIRONMENT", "us-west1-gcp")
21
+ PINECONE_INDEX_NAME = os.getenv("PINECONE_INDEX_NAME", "tiyam-chat")
22
+
23
  # اتصال به Pinecone
24
+ pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT)
25
+
26
+ # اگر ایندکس وجود نداشت بساز
27
+ if PINECONE_INDEX_NAME not in pinecone.list_indexes():
28
+ pinecone.create_index(PINECONE_INDEX_NAME, dimension=384) # بعد embedding مدل را وارد کنید
29
+
30
+ # گرفتن ایندکس
31
+ index = pinecone.Index(PINECONE_INDEX_NAME)
32
 
33
  def retrieve_answer(query, threshold=0.65, top_k=1):
34
  query_embedding = model.encode([query])[0]
 
60
  max_tokens=100,
61
  )
62
  return response['choices'][0]['message']['content'].strip()
63
+ except Exception:
64
  return "خطا در پردازش درخواست."
65
 
66
  def chat_interface(question):