Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
from deep_translator import GoogleTranslator
|
4 |
-
import torch
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
|
9 |
-
# بارگذاری مدل و توکنایزر
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
11 |
-
model = AutoModelForCausalLM.from_pretrained(
|
12 |
-
model_id,
|
13 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
14 |
-
device_map="auto"
|
15 |
-
)
|
16 |
-
model.eval()
|
17 |
-
|
18 |
-
# تابع اصلی پیشنهاد موضوع پایاننامه
|
19 |
def generate_topics(field, major, keywords, audience, level):
|
20 |
-
prompt =
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
28 |
-
with torch.no_grad():
|
29 |
-
outputs = model.generate(**inputs, max_new_tokens=256)
|
30 |
-
english_output = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
31 |
-
|
32 |
-
if not english_output:
|
33 |
-
return "<div dir='rtl' style='color: red;'>❌ مشکلی در تولید موضوعات رخ داد. لطفاً دوباره تلاش کنید.</div>"
|
34 |
-
|
35 |
-
translated_output = GoogleTranslator(source='en', target='fa').translate(english_output).strip()
|
36 |
-
translated_output_html = translated_output.replace("\n", "<br>")
|
37 |
-
|
38 |
-
html_output = (
|
39 |
-
"<div dir='rtl' style='text-align: right; font-family: Tahoma, sans-serif; "
|
40 |
-
"font-size: 16px; line-height: 1.8; min-height: 250px; max-height: 400px; "
|
41 |
-
"overflow-y: auto; border: 1px solid #ccc; padding: 10px; background-color: #fdfdfd;'>"
|
42 |
-
f"{translated_output_html}"
|
43 |
-
"<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
|
44 |
-
"<strong>021-88252497</strong></div>"
|
45 |
)
|
46 |
-
return html_output
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
iface = gr.Interface(
|
50 |
fn=generate_topics,
|
51 |
inputs=[
|
@@ -53,11 +50,10 @@ iface = gr.Interface(
|
|
53 |
gr.Textbox(label="گرایش"),
|
54 |
gr.Textbox(label="کلیدواژهها"),
|
55 |
gr.Textbox(label="جامعه هدف"),
|
56 |
-
gr.Dropdown(
|
57 |
],
|
58 |
outputs=gr.HTML(label="موضوعات پیشنهادی"),
|
59 |
-
title="🎓 پیشنهادگر موضوع پایاننامه کاسپین"
|
60 |
-
theme="default"
|
61 |
)
|
62 |
|
63 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from openai import OpenAI
|
4 |
from deep_translator import GoogleTranslator
|
|
|
5 |
|
6 |
+
# دریافت کلید از Secret Env (در Hugging Face تنظیم میکنی، پایین توضیح میدم)
|
7 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def generate_topics(field, major, keywords, audience, level):
|
10 |
+
prompt = (
|
11 |
+
f"Suggest 3 academic thesis topics based on the following:\n"
|
12 |
+
f"Field: {field}\n"
|
13 |
+
f"Specialization: {major}\n"
|
14 |
+
f"Keywords: {keywords}\n"
|
15 |
+
f"Target Audience: {audience}\n"
|
16 |
+
f"Level: {level}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
)
|
|
|
18 |
|
19 |
+
try:
|
20 |
+
completion = client.chat.completions.create(
|
21 |
+
model="gpt-4", # میتونی به gpt-3.5-turbo تغییر بدی برای ارزانتر بودن
|
22 |
+
messages=[
|
23 |
+
{"role": "system", "content": "You are an academic advisor assistant."},
|
24 |
+
{"role": "user", "content": prompt}
|
25 |
+
]
|
26 |
+
)
|
27 |
+
|
28 |
+
english_output = completion.choices[0].message.content.strip()
|
29 |
+
translated_output = GoogleTranslator(source='en', target='fa').translate(english_output)
|
30 |
+
translated_output_html = translated_output.replace("\n", "<br>")
|
31 |
+
|
32 |
+
html_output = (
|
33 |
+
"<div dir='rtl' style='text-align: right; font-family: Tahoma; font-size: 16px; "
|
34 |
+
"line-height: 1.8; min-height: 250px; max-height: 400px; overflow-y: auto; "
|
35 |
+
"border: 1px solid #ccc; padding: 10px; background-color: #fdfdfd;'>"
|
36 |
+
f"{translated_output_html}"
|
37 |
+
"<br><br>📢 برای مشاوره و راهنمایی تخصصی با گروه مشاوره کاسپین تماس بگیرید:<br>"
|
38 |
+
"<strong>021-88252497</strong></div>"
|
39 |
+
)
|
40 |
+
|
41 |
+
return html_output
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
return f"<div dir='rtl' style='color:red;'>❌ خطا در تماس با OpenAI API: {e}</div>"
|
45 |
+
|
46 |
iface = gr.Interface(
|
47 |
fn=generate_topics,
|
48 |
inputs=[
|
|
|
50 |
gr.Textbox(label="گرایش"),
|
51 |
gr.Textbox(label="کلیدواژهها"),
|
52 |
gr.Textbox(label="جامعه هدف"),
|
53 |
+
gr.Dropdown(["کارشناسی ارشد", "دکتری"], label="مقطع")
|
54 |
],
|
55 |
outputs=gr.HTML(label="موضوعات پیشنهادی"),
|
56 |
+
title="🎓 پیشنهادگر موضوع پایاننامه کاسپین"
|
|
|
57 |
)
|
58 |
|
59 |
iface.launch()
|