Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from sentence_transformers import SentenceTransformer | |
| from pinecone import Pinecone | |
| import json | |
| import os | |
| # اتصال به Pinecone | |
| api_key = "pcsk_6p6AmJ_Qua4tQN69badNHEGZTj3tt5Bd7LiyiDGcXDj92LxSaBzK2ypYxTRx2rafTEJhjL" # 🔐 کلید خودت رو اینجا بذار | |
| index_name = "tiyam-chat" | |
| region = "us-east-1" | |
| pc = Pinecone(api_key=api_key) | |
| index = pc.Index(index_name) | |
| # بارگذاری مدل | |
| model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2') | |
| # بارگذاری دادهها از JSON (فایل کنار app.py باشه) | |
| with open("tiyam_qa_data.json", "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| # تابع پاسخدهی | |
| def chatbot_fn(message, history): | |
| query_vector = model.encode(message).tolist() | |
| result = index.query(vector=query_vector, top_k=1, include_metadata=True) | |
| if result and result.matches: | |
| top_match = result.matches[0] | |
| score = top_match.score | |
| answer = top_match.metadata.get("پاسخ", "پاسخی یافت نشد.") | |
| if score < 0.6: | |
| return "متأسفم، اطلاعات کافی ندارم. لطفاً با ما تماس بگیرید." | |
| return answer | |
| else: | |
| return "متأسفم، متوجه سؤال نشدم. لطفاً واضحتر بپرسید." | |
| # رابط چت | |
| demo = gr.ChatInterface( | |
| fn=chatbot_fn, | |
| title="🤖 چتبات هوشمند تیام", | |
| description="سؤالات خود را درباره آژانس دیجیتال مارکتینگ تیام بپرسید!", | |
| theme="soft" | |
| ) | |
| demo.launch() | |