diginoron commited on
Commit
ed6acf3
·
verified ·
1 Parent(s): 3fbac70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -38
app.py CHANGED
@@ -1,51 +1,57 @@
1
  import os
2
- import gradio as gr
3
- from pinecone import Pinecone, ServerlessSpec
4
  from sentence_transformers import SentenceTransformer
5
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
6
 
7
- # بارگیری توکن‌ها از محیط
8
  HF_TOKEN = os.environ.get("HF_TOKEN")
9
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
10
  PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME")
11
 
12
- # بررسی متغیرها
13
- assert HF_TOKEN is not None, "❌ Hugging Face token missing!"
14
- assert PINECONE_API_KEY is not None, "❌ Pinecone API key missing!"
15
  assert PINECONE_INDEX_NAME is not None, "❌ Pinecone index name is missing!"
16
 
17
- # بارگذاری مدل‌های مورد نیاز
18
- embedding_model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2", token=HF_TOKEN)
19
- tokenizer = AutoTokenizer.from_pretrained("google/mt5-small", token=HF_TOKEN)
20
- language_model = AutoModelForSeq2SeqLM.from_pretrained("google/mt5-small", token=HF_TOKEN)
 
 
21
 
22
- # اتصال به Pinecone (نسخه جدید)
23
- pc = Pinecone(api_key=PINECONE_API_KEY)
24
  index = pc.Index(PINECONE_INDEX_NAME)
25
 
26
- def get_similar_context(question, top_k=1):
27
- question_embedding = embedding_model.encode(question).tolist()
28
- results = index.query(vector=question_embedding, top_k=top_k, include_metadata=True)
29
- if results and results['matches']:
30
- return results['matches'][0]['metadata']['text']
31
- return ""
32
-
33
- def generate_answer(question):
34
- context = get_similar_context(question)
35
- if context:
36
- prompt = f"پرسش: {question}\nاطلاعات مرتبط: {context}\nپاسخ:"
37
  else:
38
- prompt = f"پرسش: {question}\nپاسخ:"
39
-
40
- inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
41
- outputs = language_model.generate(**inputs, max_new_tokens=100)
42
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
43
-
44
- # رابط گرافیکی
45
- gr.Interface(
46
- fn=generate_answer,
47
- inputs=gr.Textbox(label="سؤال خود را وارد کنید", placeholder="مثلاً خدمات سئو تیام شامل چیست؟"),
48
- outputs=gr.Textbox(label="پاسخ هوشمند"),
49
- title="🤖 چت‌بات هوشمند تیام",
50
- description="با استفاده از داده‌های اختصاصی، پاسخ هوشمندانه دریافت کنید."
51
- ).launch()
 
 
 
 
 
 
 
1
  import os
2
+ import pinecone
 
3
  from sentence_transformers import SentenceTransformer
4
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
5
+ import torch
6
+ import gradio as gr
7
 
8
+ # Load environment variables
9
  HF_TOKEN = os.environ.get("HF_TOKEN")
10
  PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
11
  PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME")
12
 
13
+ assert HF_TOKEN is not None, "❌ HF token is missing!"
14
+ assert PINECONE_API_KEY is not None, "❌ Pinecone API key is missing!"
 
15
  assert PINECONE_INDEX_NAME is not None, "❌ Pinecone index name is missing!"
16
 
17
+ # Load embedding model
18
+ embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2", use_auth_token=HF_TOKEN)
19
+
20
+ # Load tokenizer and model
21
+ tokenizer = T5Tokenizer.from_pretrained("google/mt5-small", token=HF_TOKEN)
22
+ model = T5ForConditionalGeneration.from_pretrained("google/mt5-small", token=HF_TOKEN)
23
 
24
+ # Initialize Pinecone client
25
+ pc = pinecone.Pinecone(api_key=PINECONE_API_KEY)
26
  index = pc.Index(PINECONE_INDEX_NAME)
27
 
28
+ def query_index(question):
29
+ # Embed question
30
+ question_embedding = embedder.encode(question).tolist()
31
+
32
+ # Query Pinecone
33
+ results = index.query(vector=question_embedding, top_k=1, include_metadata=True)
34
+
35
+ if results.matches:
36
+ retrieved_text = results.matches[0].metadata.get("text", "")
 
 
37
  else:
38
+ retrieved_text = "متاسفم، پاسخ مناسبی پیدا نکردم."
39
+
40
+ # Generate answer
41
+ input_text = f"پرسش: {question} \n پاسخ بر اساس دانش: {retrieved_text}"
42
+ input_ids = tokenizer(input_text, return_tensors="pt", truncation=True).input_ids
43
+ output_ids = model.generate(input_ids, max_length=100)
44
+ answer = tokenizer.decode(output_ids[0], skip_special_tokens=True)
45
+
46
+ return answer
47
+
48
+ # Gradio UI
49
+ iface = gr.Interface(
50
+ fn=query_index,
51
+ inputs=gr.Textbox(label="question", placeholder="سوال خود را وارد کنید"),
52
+ outputs=gr.Textbox(label="output"),
53
+ title="چت‌بات هوشمند تیام",
54
+ description="سوالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید."
55
+ )
56
+
57
+ iface.launch(server_name="0.0.0.0", server_port=7860)