diginoron commited on
Commit
2f1aee5
·
verified ·
1 Parent(s): a741062

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -47
app.py CHANGED
@@ -1,64 +1,67 @@
1
  import os
2
  import gradio as gr
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- from sentence_transformers import SentenceTransformer
5
- from pinecone import Pinecone, ServerlessSpec
6
 
7
- # --- Load environment variables ---
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 HF_TOKEN is not None, " HF_TOKEN is missing!"
13
- assert PINECONE_API_KEY is not None, " PINECONE_API_KEY is missing!"
14
- assert PINECONE_INDEX_NAME is not None, " Pinecone index name is missing!"
15
 
16
- # --- Load models ---
17
- embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
18
- tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/gpt2-fa", token=HF_TOKEN)
19
- model = AutoModelForCausalLM.from_pretrained("HooshvareLab/gpt2-fa", token=HF_TOKEN)
20
 
21
- # --- Connect to Pinecone ---
22
- pc = Pinecone(api_key=PINECONE_API_KEY)
23
  index = pc.Index(PINECONE_INDEX_NAME)
24
 
25
- # --- Inference pipeline ---
26
- def chat(query):
27
- # Embed user question
28
- xq = embedder.encode(query).tolist()
29
-
30
- # Search in Pinecone
31
- res = index.query(vector=xq, top_k=1, include_metadata=True)
32
- matches = res.get("matches", [])
33
-
34
- if not matches:
35
- return "پاسخی برای سوال شما پیدا نشد. لطفا تماس بگیرید."
 
 
36
 
37
- # Retrieve matched content
38
- context = matches[0]['metadata']['text']
 
 
39
 
40
- # Prepare prompt
41
- prompt = f"سوال: {query}\nپاسخ بر اساس اطلاعات زیر بده: {context}\nپاسخ:"
42
- inputs = tokenizer(prompt, return_tensors="pt", padding=True)
43
 
44
- # Generate response
45
- output_ids = model.generate(**inputs, max_new_tokens=100)
46
- answer = tokenizer.decode(output_ids[0], skip_special_tokens=True)
47
 
48
- # Post-process to remove prompt
49
- if "پاسخ:" in answer:
50
- answer = answer.split("پاسخ:", 1)[-1].strip()
 
 
 
51
 
52
- return answer
 
53
 
54
- # --- Gradio UI ---
55
- with gr.Blocks(title="چت‌بات هوشمند تیام") as demo:
56
- gr.Markdown("""## چت‌بات هوشمند تیام\nسوالات خود درباره خدمات دیجیتال مارکتینگ تیام را بپرسید""")
57
- question = gr.Textbox(label="question", placeholder="سوال خود را وارد کنید")
58
- output = gr.Textbox(label="output")
59
- submit = gr.Button("Submit")
60
- submit.click(fn=chat, inputs=question, outputs=output)
61
- gr.ClearButton([question, output])
62
 
63
- if __name__ == "__main__":
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()