Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,58 @@
|
|
1 |
-
import
|
2 |
-
import pinecone
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
-
|
|
|
5 |
import torch
|
6 |
-
import
|
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 |
-
|
14 |
-
|
15 |
-
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
+
import pinecone
|
4 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
5 |
import torch
|
6 |
+
import os
|
7 |
|
8 |
+
# Load secrets and environment variables
|
|
|
9 |
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
|
10 |
PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX_NAME")
|
11 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
|
13 |
+
# Step 1: Load embedding model and Pinecone
|
14 |
+
embedding_model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
15 |
+
pinecone.init(api_key=PINECONE_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
pc = pinecone.Pinecone(api_key=PINECONE_API_KEY)
|
17 |
index = pc.Index(PINECONE_INDEX_NAME)
|
18 |
|
19 |
+
# Step 2: Load GPT-2 language model
|
20 |
+
model_name = "HooshvareLab/gpt2-fa"
|
21 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name, use_auth_token=HF_TOKEN)
|
22 |
+
model = GPT2LMHeadModel.from_pretrained(model_name, use_auth_token=HF_TOKEN)
|
23 |
+
model.eval()
|
24 |
+
|
25 |
+
# Function: Embed input and search in Pinecone
|
26 |
+
def retrieve_context(query, top_k=1):
|
27 |
+
xq = embedding_model.encode(query).tolist()
|
28 |
+
res = index.query(vector=xq, top_k=top_k, include_metadata=True)
|
29 |
+
if res.matches:
|
30 |
+
return res.matches[0].metadata['text']
|
31 |
+
return ""
|
32 |
+
|
33 |
+
# Function: Generate response using GPT-2
|
34 |
+
def generate_response(query, context):
|
35 |
+
prompt = f"پرسش: {query}\nپاسخ با توجه به اطلاعات زیر: {context}\nپاسخ:"
|
36 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True, max_length=512)
|
37 |
+
output_ids = model.generate(input_ids, max_length=256, num_beams=4, no_repeat_ngram_size=2, early_stopping=True)
|
38 |
+
output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
39 |
+
return output.split("پاسخ:")[-1].strip()
|
40 |
+
|
41 |
+
# Gradio interface
|
42 |
+
def chat(query):
|
43 |
+
context = retrieve_context(query)
|
44 |
+
response = generate_response(query, context)
|
45 |
+
return response
|
46 |
+
|
47 |
+
# UI
|
48 |
+
with gr.Blocks() as demo:
|
49 |
+
gr.Markdown("## چتبات هوشمند تیام\nسوالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید.")
|
50 |
+
with gr.Row():
|
51 |
+
inp = gr.Textbox(label="question", placeholder="سوال خود را وارد کنید")
|
52 |
+
out = gr.Textbox(label="output")
|
53 |
+
submit = gr.Button("Submit")
|
54 |
+
submit.click(chat, inputs=inp, outputs=out)
|
55 |
+
|
56 |
+
# Launch
|
57 |
+
if __name__ == "__main__":
|
58 |
+
demo.launch()
|