Spaces:
Runtime error
Runtime error
File size: 1,850 Bytes
337f19e f131275 337f19e 7be27f8 f131275 72ba832 f131275 9da05f7 72ba832 f51748e 72ba832 f51748e 7be27f8 248889e 7be27f8 f51748e f131275 f51748e f131275 f51748e f131275 72ba832 f131275 7be27f8 f131275 9da05f7 7be27f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 53 54 55 56 57 58 59 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
# بارگذاری پرسش و پاسخهای FAQ
faq_dict = {}
with open("faq.txt", encoding="utf-8") as f:
for line in f:
if ":" in line:
q, a = line.strip().split(":", 1)
faq_dict[q.strip()] = a.strip()
# بارگذاری مدل فارسی
model_name = "HooshvareLab/gpt2-fa"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
chat_history = []
# تابع پاسخدهی
def student_bot(question):
chat_history.append(f"پرسش: {question}")
prompt = """پرسش: زمان انتخاب واحد چه زمانی است؟
پاسخ: معمولا در پایان هر ترم تحصیلی، یعنی شهریور و بهمن انجام میشود.
پرسش: زمان حذف و اضافه کی هست؟
پاسخ: حدود یک هفته بعد از شروع ترم تحصیلی است.
""" + "\n".join(chat_history[-3:]) + "\nپاسخ:"
for key in faq_dict:
if key in question:
return faq_dict[key]
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=60,
pad_token_id=tokenizer.eos_token_id
)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
return answer.split("پاسخ:")[-1].strip()
# رابط Gradio
gr.Interface(
fn=student_bot,
inputs=gr.Textbox(label="❓ سوال خود را وارد کنید"),
outputs=gr.Textbox(label="✅ پاسخ هوشمند"),
title="🎓 ایجنت راهنمای دانشجویان دانشگاه",
description="پاسخ به سوالات پرتکرار آموزشی با کمک مدل FarsiGPT و پایگاه دانش",
theme="compact"
).launch()
|