Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,51 @@
|
|
1 |
import os
|
2 |
-
import pinecone
|
3 |
-
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
4 |
-
from sentence_transformers import SentenceTransformer
|
5 |
-
import torch
|
6 |
import gradio as gr
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
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 |
-
|
14 |
-
assert
|
|
|
15 |
assert PINECONE_INDEX_NAME is not None, "❌ Pinecone index name is missing!"
|
16 |
|
17 |
-
#
|
18 |
-
pinecone.init(api_key=PINECONE_API_KEY, environment="gcp-starter")
|
19 |
-
index = pinecone.Index(PINECONE_INDEX_NAME)
|
20 |
-
|
21 |
-
# --- Load models ---
|
22 |
embedding_model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2", token=HF_TOKEN)
|
23 |
-
tokenizer =
|
24 |
-
|
25 |
-
|
26 |
-
def generate_answer(question):
|
27 |
-
# Embed the question
|
28 |
-
question_embedding = embedding_model.encode(question).tolist()
|
29 |
-
|
30 |
-
# Query Pinecone for similar content
|
31 |
-
response = index.query(vector=question_embedding, top_k=3, include_metadata=True)
|
32 |
-
contexts = [match['metadata']['text'] for match in response['matches'] if 'text' in match['metadata']]
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
fn=generate_answer,
|
47 |
-
inputs=gr.Textbox(label="
|
48 |
-
outputs=gr.Textbox(label="
|
49 |
-
title="
|
50 |
-
description="
|
51 |
-
)
|
52 |
-
|
53 |
-
iface.launch()
|
|
|
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()
|
|
|
|