diginoron commited on
Commit
5c06db1
·
verified ·
1 Parent(s): 7ba95e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -1,23 +1,29 @@
 
 
1
  import os
 
2
  import json
 
3
  import gradio as gr
4
  import openai
5
  from sentence_transformers import SentenceTransformer
6
  from pinecone import Pinecone, ServerlessSpec
7
 
 
8
  openai.api_key = os.getenv("openai")
 
 
 
9
 
 
10
  model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
11
 
 
12
  with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
13
  data = json.load(f)
14
 
15
- api_key = os.getenv("PINECONE_API_KEY")
16
- region = os.getenv("PINECONE_ENVIRONMENT", "us-west1-gcp")
17
- index_name = os.getenv("PINECONE_INDEX_NAME", "tiyam-chat")
18
-
19
  pc = Pinecone(api_key=api_key)
20
-
21
  existing_indexes = pc.list_indexes().names()
22
  if index_name not in existing_indexes:
23
  pc.create_index(
@@ -29,25 +35,29 @@ if index_name not in existing_indexes:
29
  region=region
30
  )
31
  )
32
-
33
  index = pc.Index(index_name)
34
 
35
- def retrieve_answer(query, threshold=0.4, top_k=1): # <-- کاهش threshold به 0.6
 
 
 
 
 
 
 
 
36
  query_embedding = model.encode([query])[0]
37
  result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
38
-
39
  try:
40
  result_dict = result.to_dict()
41
  except Exception:
42
  result_dict = str(result)
43
-
44
  print("=== Pinecone query result ===")
45
  if isinstance(result_dict, dict):
46
  print(json.dumps(result_dict, indent=2, ensure_ascii=False))
47
  else:
48
  print(result_dict)
49
  print("============================")
50
-
51
  if hasattr(result, 'matches') and result.matches and len(result.matches) > 0 and result.matches[0].score > threshold:
52
  metadata = result.matches[0].metadata
53
  print("Matched answer:", metadata.get('answer'))
@@ -59,23 +69,27 @@ def retrieve_answer(query, threshold=0.4, top_k=1): # <-- کاهش threshold ب
59
  def generate_human_response(context_text):
60
  if not context_text:
61
  return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."
62
-
 
63
  prompt = (
64
  f"این متن پاسخ سوال مشتری است: \"{context_text}\".\n"
65
- "لطفاً یک پاسخ کوتاه، رسمی و کاملاً مختصر و مفید به زبان فارسی تولید کن که فقط بر اساس همین متن باشد و هیچ اطلاعات اضافی نده."
66
  )
67
-
68
  try:
69
  response = openai.chat.completions.create(
70
- model="gpt-4",
71
  messages=[
72
  {"role": "system", "content": "شما یک پاسخگوی رسمی شرکت هستید."},
73
  {"role": "user", "content": prompt}
74
  ],
75
  temperature=0.2,
76
- max_tokens=100,
77
  )
78
- return response.choices[0].message.content.strip()
 
 
 
 
79
  except Exception as e:
80
  print("OpenAI API error:", e)
81
  return "خطا در پردازش درخواست."
@@ -94,4 +108,4 @@ demo = gr.Interface(
94
  )
95
 
96
  if __name__ == "__main__":
97
- demo.launch()
 
1
+ import pandas as pd
2
+ import numpy as np
3
  import os
4
+ import sys
5
  import json
6
+ import pickle
7
  import gradio as gr
8
  import openai
9
  from sentence_transformers import SentenceTransformer
10
  from pinecone import Pinecone, ServerlessSpec
11
 
12
+ # تنظیم کلیدهای API
13
  openai.api_key = os.getenv("openai")
14
+ api_key = os.getenv("PINECONE_API_KEY")
15
+ region = os.getenv("PINECONE_ENVIRONMENT", "us-west1-gcp")
16
+ index_name = os.getenv("PINECONE_INDEX_NAME", "tiyam-chat")
17
 
18
+ # بارگذاری مدل
19
  model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
20
 
21
+ # بارگذاری داده‌ها (در حال حاضر فقط لود می‌شه، باید به Pinecone اضافه بشه)
22
  with open("tiyam_qa_data.json", "r", encoding="utf-8") as f:
23
  data = json.load(f)
24
 
25
+ # اتصال به Pinecone
 
 
 
26
  pc = Pinecone(api_key=api_key)
 
27
  existing_indexes = pc.list_indexes().names()
28
  if index_name not in existing_indexes:
29
  pc.create_index(
 
35
  region=region
36
  )
37
  )
 
38
  index = pc.Index(index_name)
39
 
40
+ # سیستم کش
41
+ cache_file = "chat_cache.pkl"
42
+ try:
43
+ with open(cache_file, "rb") as f:
44
+ cache = pickle.load(f)
45
+ except FileNotFoundError:
46
+ cache = {}
47
+
48
+ def retrieve_answer(query, threshold=0.6, top_k=1): # افزایش threshold به 0.6
49
  query_embedding = model.encode([query])[0]
50
  result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
 
51
  try:
52
  result_dict = result.to_dict()
53
  except Exception:
54
  result_dict = str(result)
 
55
  print("=== Pinecone query result ===")
56
  if isinstance(result_dict, dict):
57
  print(json.dumps(result_dict, indent=2, ensure_ascii=False))
58
  else:
59
  print(result_dict)
60
  print("============================")
 
61
  if hasattr(result, 'matches') and result.matches and len(result.matches) > 0 and result.matches[0].score > threshold:
62
  metadata = result.matches[0].metadata
63
  print("Matched answer:", metadata.get('answer'))
 
69
  def generate_human_response(context_text):
70
  if not context_text:
71
  return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."
72
+ if context_text in cache:
73
+ return cache[context_text]
74
  prompt = (
75
  f"این متن پاسخ سوال مشتری است: \"{context_text}\".\n"
76
+ "لطفاً یک پاسخ کوتاه، رسمی و کاملاً مختصر و مفید به زبان فارسی تولید کن که فقط بر اساس همین متن باشد."
77
  )
 
78
  try:
79
  response = openai.chat.completions.create(
80
+ model="gpt-3.5-turbo", # مدل ارزان‌تر
81
  messages=[
82
  {"role": "system", "content": "شما یک پاسخگوی رسمی شرکت هستید."},
83
  {"role": "user", "content": prompt}
84
  ],
85
  temperature=0.2,
86
+ max_tokens=50, # کاهش توکن‌ها
87
  )
88
+ answer = response.choices[0].message.content.strip()
89
+ cache[context_text] = answer
90
+ with open(cache_file, "wb") as f:
91
+ pickle.dump(cache, f)
92
+ return answer
93
  except Exception as e:
94
  print("OpenAI API error:", e)
95
  return "خطا در پردازش درخواست."
 
108
  )
109
 
110
  if __name__ == "__main__":
111
+ demo.launch()