File size: 2,193 Bytes
0f5d2ed
ed6acf3
3fbac70
ed6acf3
 
 
281c0ae
ed6acf3
0f5d2ed
012badc
 
74c93be
ed6acf3
 
7279042
 
ed6acf3
 
 
 
 
 
d95ee14
ed6acf3
 
3fbac70
d95ee14
ed6acf3
 
 
 
 
 
 
 
 
3fbac70
ed6acf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pinecone
from sentence_transformers import SentenceTransformer
from transformers import T5Tokenizer, T5ForConditionalGeneration
import torch
import gradio as gr

# Load environment variables
HF_TOKEN = os.environ.get("HF_TOKEN")
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME")

assert HF_TOKEN is not None, "❌ HF token is missing!"
assert PINECONE_API_KEY is not None, "❌ Pinecone API key is missing!"
assert PINECONE_INDEX_NAME is not None, "❌ Pinecone index name is missing!"

# Load embedding model
embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2", use_auth_token=HF_TOKEN)

# Load tokenizer and model
tokenizer = T5Tokenizer.from_pretrained("google/mt5-small", token=HF_TOKEN)
model = T5ForConditionalGeneration.from_pretrained("google/mt5-small", token=HF_TOKEN)

# Initialize Pinecone client
pc = pinecone.Pinecone(api_key=PINECONE_API_KEY)
index = pc.Index(PINECONE_INDEX_NAME)

def query_index(question):
    # Embed question
    question_embedding = embedder.encode(question).tolist()

    # Query Pinecone
    results = index.query(vector=question_embedding, top_k=1, include_metadata=True)

    if results.matches:
        retrieved_text = results.matches[0].metadata.get("text", "")
    else:
        retrieved_text = "متاسفم، پاسخ مناسبی پیدا نکردم."

    # Generate answer
    input_text = f"پرسش: {question} \n پاسخ بر اساس دانش: {retrieved_text}"
    input_ids = tokenizer(input_text, return_tensors="pt", truncation=True).input_ids
    output_ids = model.generate(input_ids, max_length=100)
    answer = tokenizer.decode(output_ids[0], skip_special_tokens=True)

    return answer

# Gradio UI
iface = gr.Interface(
    fn=query_index,
    inputs=gr.Textbox(label="question", placeholder="سوال خود را وارد کنید"),
    outputs=gr.Textbox(label="output"),
    title="چت‌بات هوشمند تیام",
    description="سوالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید."
)

iface.launch(server_name="0.0.0.0", server_port=7860)