Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,57 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
from pinecone import Pinecone, ServerlessSpec
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
-
from transformers import
|
|
|
|
|
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
|
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 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
pc = Pinecone(api_key=PINECONE_API_KEY)
|
24 |
index = pc.Index(PINECONE_INDEX_NAME)
|
25 |
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if context:
|
36 |
-
prompt = f"پرسش: {question}\nاطلاعات مرتبط: {context}\nپاسخ:"
|
37 |
else:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|