Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
import
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
import torch
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
11 |
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
|
12 |
PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME")
|
@@ -15,50 +14,40 @@ assert HF_TOKEN is not None, "❌ HF_TOKEN is missing!"
|
|
15 |
assert PINECONE_API_KEY is not None, "❌ Pinecone API key is missing!"
|
16 |
assert PINECONE_INDEX_NAME is not None, "❌ Pinecone index name is missing!"
|
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 |
-
return raw_answer
|
53 |
-
final_answer = rewrite_answer(question, raw_answer)
|
54 |
-
return final_answer
|
55 |
-
|
56 |
-
demo = gr.Interface(
|
57 |
-
fn=chat_interface,
|
58 |
-
inputs="text",
|
59 |
-
outputs="text",
|
60 |
-
title="چتبات تیام",
|
61 |
-
description="پرسش خود را درباره خدمات دیجیتال مارکتینگ تیام بپرسید."
|
62 |
)
|
63 |
|
64 |
-
|
|
|
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 |
+
# --- 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")
|
|
|
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 |
+
# --- Initialize Pinecone ---
|
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 = T5Tokenizer.from_pretrained("google/mt5-small", token=HF_TOKEN)
|
24 |
+
model = T5ForConditionalGeneration.from_pretrained("google/mt5-small", token=HF_TOKEN)
|
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 |
+
# Concatenate context
|
35 |
+
context = "\n".join(contexts)
|
36 |
+
input_text = f"پرسش: {question}\nاطلاعات: {context}\nپاسخ:"
|
37 |
+
|
38 |
+
# Tokenize and generate
|
39 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512)
|
40 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
41 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
+
return answer
|
43 |
+
|
44 |
+
# --- Gradio UI ---
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=generate_answer,
|
47 |
+
inputs=gr.Textbox(label="question"),
|
48 |
+
outputs=gr.Textbox(label="output"),
|
49 |
+
title="💬 چتبات هوشمند تیام",
|
50 |
+
description="سؤالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
|
53 |
+
iface.launch()
|