Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Load the models and tokenizers
|
| 6 |
+
tokenizer1 = AutoTokenizer.from_pretrained("JasperV13/Fhamator-30000")
|
| 7 |
+
model1 = AutoModelForCausalLM.from_pretrained("JasperV13/Fhamator-30000")
|
| 8 |
+
|
| 9 |
+
tokenizer2 = AutoTokenizer.from_pretrained("JasperV13/Fhamator-SFT")
|
| 10 |
+
model2 = AutoModelForCausalLM.from_pretrained("JasperV13/Fhamator-SFT")
|
| 11 |
+
|
| 12 |
+
def generate_text_fhamator(input_text, max_length, num_return_sequences, no_repeat_ngram_size, top_k, top_p, temperature):
|
| 13 |
+
input_ids = tokenizer1.encode(input_text, return_tensors='pt')
|
| 14 |
+
output = model1.generate(
|
| 15 |
+
input_ids,
|
| 16 |
+
max_length=max_length,
|
| 17 |
+
num_return_sequences=num_return_sequences,
|
| 18 |
+
no_repeat_ngram_size=no_repeat_ngram_size,
|
| 19 |
+
top_k=top_k,
|
| 20 |
+
top_p=top_p,
|
| 21 |
+
temperature=temperature,
|
| 22 |
+
do_sample=True
|
| 23 |
+
)
|
| 24 |
+
generated_texts = [tokenizer1.decode(output[i], skip_special_tokens=True) for i in range(num_return_sequences)]
|
| 25 |
+
return "\n\n".join(generated_texts)
|
| 26 |
+
|
| 27 |
+
def generate_text_sft(input_text, max_length, num_return_sequences, no_repeat_ngram_size, top_k, top_p, temperature):
|
| 28 |
+
inputs = tokenizer2(input_text, return_tensors="pt")
|
| 29 |
+
output = model2.generate(
|
| 30 |
+
inputs['input_ids'],
|
| 31 |
+
max_length=max_length,
|
| 32 |
+
num_return_sequences=num_return_sequences,
|
| 33 |
+
no_repeat_ngram_size=no_repeat_ngram_size,
|
| 34 |
+
top_k=top_k,
|
| 35 |
+
top_p=top_p,
|
| 36 |
+
temperature=temperature,
|
| 37 |
+
do_sample=True
|
| 38 |
+
)
|
| 39 |
+
generated_texts = [tokenizer2.decode(output[i], skip_special_tokens=True) for i in range(num_return_sequences)]
|
| 40 |
+
return "\n\n".join(generated_texts)
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
with gr.Row():
|
| 44 |
+
gr.Markdown("## 🤖✨ Fhamator: The Darija-Speaking Model")
|
| 45 |
+
|
| 46 |
+
with gr.Tab("📖 Explanation"):
|
| 47 |
+
gr.Markdown("""
|
| 48 |
+
# 📚 Explanation of the Work Done
|
| 49 |
+
Welcome to the **Fhamator** application! This app consists of three main tabs, each with a unique purpose:
|
| 50 |
+
|
| 51 |
+
1. **🔍 Explanation**: Provides an overview of what this app does and how it works.
|
| 52 |
+
2. **🧠 Fhamator Model**: Test the base 'Fhamator-30000' language model and tweak its hyperparameters to see how it performs.
|
| 53 |
+
3. **🚀 Fhamator-SFT Model**: Experiment with the fine-tuned 'Fhamator-SFT' model, designed for more specific tasks, also with customizable hyperparameters.
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
with gr.Tab("Test Fhamator-30000 Model"):
|
| 57 |
+
with gr.Group():
|
| 58 |
+
input_text = gr.Textbox(label="Input Prompt", value="المغرب هو دولة معروفة ب")
|
| 59 |
+
max_length = gr.Slider(50, 200, value=100, step=1, label="Max Length")
|
| 60 |
+
num_return_sequences = gr.Slider(1, 5, value=3, step=1, label="Number of Sequences")
|
| 61 |
+
no_repeat_ngram_size = gr.Slider(1, 5, value=2, step=1, label="No Repeat N-Gram Size")
|
| 62 |
+
top_k = gr.Slider(1, 100, value=50, step=1, label="Top K")
|
| 63 |
+
top_p = gr.Slider(0.0, 1.0, value=0.95, step=0.01, label="Top P")
|
| 64 |
+
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 65 |
+
output_text = gr.Textbox(label="Generated Texts", lines=8)
|
| 66 |
+
generate_btn = gr.Button("Generate")
|
| 67 |
+
|
| 68 |
+
generate_btn.click(
|
| 69 |
+
generate_text_fhamator,
|
| 70 |
+
inputs=[input_text, max_length, num_return_sequences, no_repeat_ngram_size, top_k, top_p, temperature],
|
| 71 |
+
outputs=output_text
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with gr.Tab("Test Fhamator-SFT Model"):
|
| 75 |
+
with gr.Group():
|
| 76 |
+
input_text_sft = gr.Textbox(label="Instruction", value="السؤال :س - التاريخ المغربي؟\n\n: الجواب\n")
|
| 77 |
+
max_length = gr.Slider(50, 200, value=60, step=1, label="Max Length")
|
| 78 |
+
num_return_sequences = gr.Slider(1, 5, value=5, step=1, label="Number of Sequences")
|
| 79 |
+
no_repeat_ngram_size = gr.Slider(1, 5, value=2, step=1, label="No Repeat N-Gram Size")
|
| 80 |
+
top_k_sft = gr.Slider(1, 100, value=50, step=1, label="Top K")
|
| 81 |
+
top_p_sft = gr.Slider(0.0, 1.0, value=0.95, step=0.01, label="Top P")
|
| 82 |
+
temperature_sft = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 83 |
+
output_text_sft = gr.Textbox(label="Generated Texts", lines=8)
|
| 84 |
+
generate_btn_sft = gr.Button("Generate")
|
| 85 |
+
|
| 86 |
+
generate_btn_sft.click(
|
| 87 |
+
generate_text_sft,
|
| 88 |
+
inputs=[input_text_sft, max_length, num_return_sequences, no_repeat_ngram_size, top_k_sft, top_p_sft, temperature_sft],
|
| 89 |
+
outputs=output_text_sft
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
demo.launch(debug = True)
|