Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import os
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
|
7 |
+
|
8 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
9 |
+
print(f'[INFO] Using device: {device}')
|
10 |
+
|
11 |
+
# token
|
12 |
+
token = os.environ['TOKEN']
|
13 |
+
|
14 |
+
# Load the pretrained model and tokenizer
|
15 |
+
MODEL_NAME = "atlasia/Al-Atlas-LLM"
|
16 |
+
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=token)
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, token=token).to(device)
|
19 |
+
|
20 |
+
# Predefined examples
|
21 |
+
examples = [
|
22 |
+
["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر اللي كيركز"
|
23 |
+
, 256, 0.7, 0.9, 150, 8, 1.5],
|
24 |
+
["المستقبل ديال الذكاء الصناعي فالمغرب"
|
25 |
+
, 256, 0.7, 0.9, 150, 8, 1.5],
|
26 |
+
[" المطبخ المغربي"
|
27 |
+
, 256, 0.7, 0.9, 150, 8, 1.5],
|
28 |
+
["الماكلة المغربية كتعتبر من أحسن الماكلات فالعالم"
|
29 |
+
, 256, 0.7, 0.9, 150, 8, 1.5],
|
30 |
+
]
|
31 |
+
|
32 |
+
@spaces.GPU
|
33 |
+
def generate_text(prompt, max_length=256, temperature=0.7, top_p=0.9, top_k=150, num_beams=8, repetition_penalty=1.5):
|
34 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
35 |
+
output = model.generate(
|
36 |
+
**inputs,
|
37 |
+
max_length=max_length,
|
38 |
+
temperature=temperature,
|
39 |
+
top_p=top_p,
|
40 |
+
do_sample=True,
|
41 |
+
repetition_penalty=repetition_penalty,
|
42 |
+
num_beams=num_beams,
|
43 |
+
top_k= top_k,
|
44 |
+
early_stopping = True,
|
45 |
+
)
|
46 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
# Create the Gradio interface
|
50 |
+
with gr.Blocks() as app:
|
51 |
+
gr.Interface(
|
52 |
+
fn=generate_text,
|
53 |
+
inputs=[
|
54 |
+
gr.Textbox(label="Prompt: دخل النص بالدارجة"),
|
55 |
+
gr.Slider(50, 4096, value=256, label="Max Length"),
|
56 |
+
gr.Slider(0.1, 1.5, value=0.7, label="Temperature"),
|
57 |
+
gr.Slider(0.1, 1.0, value=0.9, label="Top-p"),
|
58 |
+
gr.Slider(1, 10000, value=150, label="Top-k"),
|
59 |
+
gr.Slider(1, 20, value=8, label="Number of Beams"),
|
60 |
+
gr.Slider(0.0, 100.0, value=1.5, label="Repetition Penalty"),
|
61 |
+
],
|
62 |
+
outputs=gr.Textbox(label="Generated Text in Moroccan Darija"),
|
63 |
+
title="Moroccan Darija LLM",
|
64 |
+
description="Enter a prompt and get AI-generated text using our pretrained LLM on Moroccan Darija.",
|
65 |
+
examples=examples,
|
66 |
+
)
|
67 |
+
|
68 |
+
app.launch()
|