Spaces:
Sleeping
Sleeping
File size: 3,469 Bytes
f957bce 281c0ae f957bce 281c0ae 79ad868 281c0ae e1de535 281c0ae 74c93be 281c0ae 7291a91 281c0ae 1ba9ac5 281c0ae 79ad868 1ba9ac5 79ad868 1ba9ac5 79ad868 be823ee 281c0ae e4cb7b4 281c0ae f957bce 35e4839 f957bce 281c0ae f957bce 281c0ae 7291a91 281c0ae f957bce 281c0ae 7291a91 f957bce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# app.py
import json
import gradio as gr
from sentence_transformers import SentenceTransformer
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from pinecone import Pinecone
import os
# ===============================
# 🌐 اتصال به Pinecone
# ===============================
PINECONE_API_KEY = "pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL"
PINECONE_INDEX_NAME = "tiyam-chat"
pc = Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(PINECONE_INDEX_NAME)
# ===============================
# 🔤 بارگذاری مدل embedding
# ===============================
embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
# ===============================
# 🧠 بارگذاری مدل google/mt5-small برای بازنویسی
# ===============================
hf_token = os.getenv("HF_TOKEN")
tokenizer = AutoTokenizer.from_pretrained(
"google/mt5-small",
use_auth_token=hf_token
)
mt5_model = AutoModelForSeq2SeqLM.from_pretrained(
"google/mt5-small",
use_auth_token=hf_token
)
# ===============================
# 🔍 بازیابی پاسخ از Pinecone
# ===============================
def retrieve_answer(query, threshold=0.65, top_k=1):
query_embedding = embedding_model.encode([query])[0]
result = index.query(vector=query_embedding.tolist(), top_k=top_k, include_metadata=True)
if result['matches'] and result['matches'][0]['score'] > threshold:
return result['matches'][0]['metadata'].get('answer', 'پاسخ یافت نشد')
else:
return None
# ===============================
# ✨ بازنویسی پاسخ با MT5
# ===============================
def rewrite_with_mt5(answer, question):
prompt = f"پاسخ به سوال: {question} بر اساس اطلاعات: {answer}"
input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True)
output_ids = mt5_model.generate(input_ids, max_new_tokens=60)
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
# ===============================
# 💬 منطق نهایی پاسخدهی
# ===============================
def final_answer(user_question):
answer = retrieve_answer(user_question)
if answer:
return rewrite_with_mt5(answer, user_question)
else:
# پاسخ عمومی برای سوالات چتی یا ناموجود
general_prompts = {
"سلام": "سلام! خوشحالیم که اینجایی 😊",
"خوبی؟": "مرسی! من خوبم، شما چطورید؟",
"مرسی": "خواهش میکنم، در خدمت شما هستیم."
}
for key in general_prompts:
if key in user_question:
return general_prompts[key]
return "متأسفم، پاسخ دقیقی برای این سوال نداریم. لطفاً با ما تماس بگیرید."
# ===============================
# 🎛️ رابط Gradio
# ===============================
demo = gr.Interface(
fn=final_answer,
inputs=gr.Textbox(lines=2, label="سؤال شما"),
outputs=gr.Textbox(label="پاسخ تیام"),
title="💬 چتبات هوشمند تیام",
description="سؤالات خود را از آژانس دیجیتال مارکتینگ تیام بپرسید. سیستم از ترکیب جستجوی دقیق و بازنویسی طبیعی استفاده میکند."
)
demo.launch()
|