Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# بارگذاری مدل تشابه معنایی
|
| 5 |
embedder = SentenceTransformer("myrkur/sentence-transformer-parsbert-fa-2.0")
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
faq_dict = {
|
| 9 |
"زمان انتخاب واحد": "معمولاً پایان شهریور و بهمن است.",
|
| 10 |
"زمان حذف و اضافه": "حدود یک هفته پس از شروع ترم تحصیلی است.",
|
|
@@ -16,26 +22,41 @@ faq_dict = {
|
|
| 16 |
faq_questions = list(faq_dict.keys())
|
| 17 |
faq_embeddings = embedder.encode(faq_questions, convert_to_tensor=True)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=student_bot,
|
| 36 |
inputs=gr.Textbox(label="سؤال خود را وارد کنید"),
|
| 37 |
outputs=gr.Textbox(label="پاسخ"),
|
| 38 |
-
title="ایجنت راهنمای دانشجویان",
|
| 39 |
-
description="
|
| 40 |
)
|
|
|
|
| 41 |
iface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# بارگذاری مدل SBERT برای تشابه معنایی
|
| 7 |
embedder = SentenceTransformer("myrkur/sentence-transformer-parsbert-fa-2.0")
|
| 8 |
|
| 9 |
+
# بارگذاری مدل زبانی GPT فارسی
|
| 10 |
+
gpt_tokenizer = AutoTokenizer.from_pretrained("HooshvareLab/gpt2-fa")
|
| 11 |
+
gpt_model = AutoModelForCausalLM.from_pretrained("HooshvareLab/gpt2-fa")
|
| 12 |
+
|
| 13 |
+
# پایگاه داده سوالات متداول
|
| 14 |
faq_dict = {
|
| 15 |
"زمان انتخاب واحد": "معمولاً پایان شهریور و بهمن است.",
|
| 16 |
"زمان حذف و اضافه": "حدود یک هفته پس از شروع ترم تحصیلی است.",
|
|
|
|
| 22 |
faq_questions = list(faq_dict.keys())
|
| 23 |
faq_embeddings = embedder.encode(faq_questions, convert_to_tensor=True)
|
| 24 |
|
| 25 |
+
# تابع پاسخدهی مدل زبانی Fallback
|
| 26 |
+
def generate_with_farsigpt(question, max_length=80):
|
| 27 |
+
try:
|
| 28 |
+
input_ids = gpt_tokenizer.encode(question, return_tensors="pt")
|
| 29 |
+
output = gpt_model.generate(input_ids, max_length=max_length, pad_token_id=gpt_tokenizer.eos_token_id)
|
| 30 |
+
response = gpt_tokenizer.decode(output[0], skip_special_tokens=True)
|
| 31 |
+
return response[len(question):].strip()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"❗️خطا در پاسخدهی با مدل زبانی: {str(e)}"
|
| 34 |
|
| 35 |
+
# تابع کلی پاسخدهی ایجنت
|
| 36 |
+
def student_bot(question):
|
| 37 |
+
try:
|
| 38 |
+
question_embedding = embedder.encode(question, convert_to_tensor=True)
|
| 39 |
+
cos_scores = util.pytorch_cos_sim(question_embedding, faq_embeddings)[0]
|
| 40 |
+
best_score = cos_scores.max().item()
|
| 41 |
+
best_idx = cos_scores.argmax().item()
|
| 42 |
|
| 43 |
+
if best_score >= 0.7:
|
| 44 |
+
best_q = faq_questions[best_idx]
|
| 45 |
+
return faq_dict[best_q]
|
| 46 |
+
else:
|
| 47 |
+
response = generate_with_farsigpt(question)
|
| 48 |
+
return f"🤖 پاسخ با مدل زبانی:\n{response}"
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"❗️خطا در سیستم: {str(e)}"
|
| 51 |
|
| 52 |
+
# رابط کاربری Gradio
|
| 53 |
iface = gr.Interface(
|
| 54 |
fn=student_bot,
|
| 55 |
inputs=gr.Textbox(label="سؤال خود را وارد کنید"),
|
| 56 |
outputs=gr.Textbox(label="پاسخ"),
|
| 57 |
+
title="ایجنت راهنمای دانشجویان با fallback",
|
| 58 |
+
description="اول از پایگاه دانش، در صورت نبودن، با مدل زبانی پاسخ داده میشود."
|
| 59 |
)
|
| 60 |
+
|
| 61 |
iface.launch()
|
| 62 |
+
|