File size: 2,434 Bytes
445446a
2322024
445446a
2322024
077bef3
445446a
 
2322024
445446a
 
5eb89a3
094abed
2322024
 
 
97595a0
445446a
5eb89a3
97595a0
5eb89a3
2322024
5eb89a3
2322024
5eb89a3
97595a0
5eb89a3
97595a0
5eb89a3
97595a0
 
e472282
d48d1c6
445446a
d48d1c6
 
0d6d035
d48d1c6
 
 
 
 
 
 
3148133
aa88648
0d6d035
d48d1c6
 
aa88648
d48d1c6
f797d51
 
d48d1c6
f797d51
d48d1c6
f797d51
d48d1c6
 
445446a
 
 
7aab66c
445446a
2322024
 
 
 
d48d1c6
 
2322024
3148133
 
7aab66c
 
2322024
7aab66c
2322024
0d6d035
3148133
0d6d035
7aab66c
445446a
 
f797d51
2322024
445446a
 
 
 
0d6d035
aa88648
d48d1c6
 
aa88648
 
1c206a7
0d6d035
 
 
1d0de72
0d6d035
1c206a7
 
 
0d6d035
 
 
51f1f7f
0d6d035
 
 
 
51f1f7f
d48d1c6
 
0d6d035
 
d48d1c6
0d6d035
d48d1c6
0d6d035
445446a
d48d1c6
 
0d6d035
 
 
 
445446a
d48d1c6
 
 
 
 
 
 
 
 
 
 
0d6d035
 
 
2322024
0d6d035
5eb89a3
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gradio as gr
import spaces
import torch

from transformers import AutoTokenizer, AutoModelForCausalLM


MODEL_ID = "ddfws/Rezaeian-StatsAI"


print("Loading tokenizer...")

tokenizer = AutoTokenizer.from_pretrained(
    MODEL_ID
)


print("Loading model...")

model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    device_map="auto",
    dtype=torch.float16
)

model.eval()

print("MODEL READY")



def format_answer(text):

    # مرتب سازی خروجی فارسی
    text = text.replace("\n", "<br>")

    # فرمول های LaTeX
    text = text.replace("\\[", "<div class='formula'>")
    text = text.replace("\\]", "</div>")

    text = text.replace("$$", "<div class='formula'>")

    return text



@spaces.GPU
def generate(message, history):

    prompt = ""


    for user, assistant in history:

        prompt += f"User: {user}\nAssistant: {assistant}\n"


    prompt += f"User: {message}\nAssistant:"


    inputs = tokenizer(
        prompt,
        return_tensors="pt"
    )


    inputs = {
        k: v.to(model.device)
        for k, v in inputs.items()
    }


    with torch.no_grad():

        output = model.generate(
            **inputs,
            max_new_tokens=512,
            temperature=0.7,
            top_p=0.9,
            do_sample=True
        )


    result = tokenizer.decode(
        output[0],
        skip_special_tokens=True
    )


    answer = result.split("Assistant:")[-1]


    return format_answer(answer)



demo = gr.ChatInterface(
    fn=generate,
    title="Rezaeian StatsAI",
    description="دستیار هوشمند آمار و احتمال مهندسی"
)



demo.launch(
    server_name="0.0.0.0",
    css="""

    .gradio-container {
        direction: rtl;
        max-width: 1200px !important;
    }


    .message {
        direction: rtl !important;
        text-align: right !important;
        font-family: Tahoma, Arial, sans-serif !important;
        font-size:18px !important;
        line-height:2;
    }


    textarea {
        direction: rtl !important;
        text-align:right !important;
        font-size:18px !important;
    }


    .formula {
        direction:ltr !important;
        text-align:center !important;
        font-family: Cambria Math, Times New Roman, serif !important;
        font-size:20px !important;
        padding:12px;
        margin:12px;
    }


    footer {
        display:none !important;
    }

    """
)