Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,67 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
from
|
5 |
-
from
|
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 |
-
assert
|
13 |
-
assert
|
14 |
-
assert
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/gpt2-fa", token=HF_TOKEN)
|
19 |
-
model = AutoModelForCausalLM.from_pretrained("HooshvareLab/gpt2-fa", token=HF_TOKEN)
|
20 |
|
21 |
-
#
|
22 |
-
pc = Pinecone(api_key=PINECONE_API_KEY)
|
23 |
index = pc.Index(PINECONE_INDEX_NAME)
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
inputs = tokenizer(prompt, return_tensors="pt", padding=True)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
answer = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
demo.launch()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import pinecone
|
4 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
5 |
+
from sentence_transformers import SentenceTransformer, util
|
6 |
|
7 |
+
# Environment variables
|
|
|
8 |
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
|
9 |
PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME")
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
11 |
|
12 |
+
assert PINECONE_API_KEY is not None, "\u274c PINECONE_API_KEY is missing!"
|
13 |
+
assert PINECONE_INDEX_NAME is not None, "\u274c PINECONE_INDEX_NAME is missing!"
|
14 |
+
assert HF_TOKEN is not None, "\u274c HF_TOKEN is missing!"
|
15 |
|
16 |
+
# Load embedding model
|
17 |
+
embedding_model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
|
|
|
|
18 |
|
19 |
+
# Init Pinecone
|
20 |
+
pc = pinecone.Pinecone(api_key=PINECONE_API_KEY)
|
21 |
index = pc.Index(PINECONE_INDEX_NAME)
|
22 |
|
23 |
+
# Load language model and tokenizer
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/gpt2-fa")
|
25 |
+
model = AutoModelForCausalLM.from_pretrained("HooshvareLab/gpt2-fa")
|
26 |
+
text_generator = pipeline(
|
27 |
+
"text-generation",
|
28 |
+
model=model,
|
29 |
+
tokenizer=tokenizer,
|
30 |
+
max_length=100,
|
31 |
+
do_sample=True,
|
32 |
+
top_p=0.95,
|
33 |
+
temperature=0.8,
|
34 |
+
return_full_text=False,
|
35 |
+
)
|
36 |
|
37 |
+
def generate_answer(question):
|
38 |
+
try:
|
39 |
+
# Step 1: Embed the question
|
40 |
+
question_embedding = embedding_model.encode(question).tolist()
|
41 |
|
42 |
+
# Step 2: Search similar questions in Pinecone
|
43 |
+
search_result = index.query(vector=question_embedding, top_k=1, include_metadata=True)
|
|
|
44 |
|
45 |
+
if search_result and search_result.matches:
|
46 |
+
best_match = search_result.matches[0].metadata.get("answer", "")
|
|
|
47 |
|
48 |
+
# Step 3: Rewrite with the language model
|
49 |
+
prompt = f"پرسش: {question}\nپاسخ: {best_match}\nپاسخ نهایی:"
|
50 |
+
output = text_generator(prompt, max_new_tokens=50)[0]["generated_text"]
|
51 |
+
return output.strip()
|
52 |
+
else:
|
53 |
+
return "پاسخی برای این پرسش در پایگاه داده یافت نشد. لطفاً با پشتیبانی تماس بگیرید."
|
54 |
|
55 |
+
except Exception as e:
|
56 |
+
return f"خطا: {str(e)}"
|
57 |
|
58 |
+
# Gradio Interface
|
59 |
+
demo = gr.Interface(
|
60 |
+
fn=generate_answer,
|
61 |
+
inputs=gr.Textbox(label="question", placeholder="سوال خود را وارد کنید..."),
|
62 |
+
outputs=gr.Textbox(label="output"),
|
63 |
+
title="چتبات هوشمند تیام",
|
64 |
+
description="سوالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید."
|
65 |
+
)
|
66 |
|
67 |
+
demo.launch()
|
|