Spaces:
Sleeping
Sleeping
Commit
·
a1918ea
1
Parent(s):
90aa72c
Add application file
Browse files- app.py +101 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
model_name = "eljanmahammadli/AzLlama-152M-Alpaca"
|
| 6 |
+
model = pipeline("text-generation", model=model_name, torch_dtype=torch.float16)
|
| 7 |
+
logo_path = "/Users/eljan/Documents/AzLlama/AzLlama-logo.webp"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def get_prompt(question):
|
| 11 |
+
base_instruction = "Aşağıda tapşırığı təsvir edən təlimat və əlavə kontekst təmin edən giriş verilmiştir. Sorğunu uyğun şəkildə tamamlayan cavab yazın."
|
| 12 |
+
prompt = f"""{base_instruction}
|
| 13 |
+
|
| 14 |
+
### Təlimat:
|
| 15 |
+
{question}
|
| 16 |
+
|
| 17 |
+
### Cavab:
|
| 18 |
+
"""
|
| 19 |
+
return prompt
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_answer(llm_output):
|
| 23 |
+
return llm_output.split("### Cavab:")[1].strip()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def answer_question(history, temperature, top_p, repetition_penalty, top_k, question):
|
| 27 |
+
model_params = {
|
| 28 |
+
"temperature": temperature,
|
| 29 |
+
"top_p": top_p,
|
| 30 |
+
"repetition_penalty": repetition_penalty,
|
| 31 |
+
"top_k": top_k,
|
| 32 |
+
"max_length": 512, # Adjust based on your needs
|
| 33 |
+
"do_sample": True,
|
| 34 |
+
}
|
| 35 |
+
prompt = get_prompt(question)
|
| 36 |
+
llm_output = model(prompt, **model_params)[0]
|
| 37 |
+
answer = get_answer(llm_output["generated_text"])
|
| 38 |
+
divider = "\n\n" if history else ""
|
| 39 |
+
print(answer)
|
| 40 |
+
new_history = history + divider + f"USER: {question}\nASSISTANT: {answer}\n"
|
| 41 |
+
return new_history, "" # Return updated history and clear the question input
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def send_action(_=None):
|
| 45 |
+
send_button.click()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as app:
|
| 49 |
+
gr.Markdown("# AzLlama-150M Chatbot\n\n")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column(scale=0.2, min_width=200):
|
| 53 |
+
gr.Markdown("### Model Logo")
|
| 54 |
+
gr.Image(
|
| 55 |
+
value=logo_path,
|
| 56 |
+
)
|
| 57 |
+
# write info about the model
|
| 58 |
+
gr.Markdown(
|
| 59 |
+
"### Model Info\n"
|
| 60 |
+
"This model is a 150M paramater LLaMA2 model trained from scratch on Azerbaijani text. It can be used to generate text based on the given prompt. "
|
| 61 |
+
)
|
| 62 |
+
with gr.Column(scale=0.6):
|
| 63 |
+
gr.Markdown("### Chat with the Assistant")
|
| 64 |
+
history = gr.Textbox(
|
| 65 |
+
label="Chat History", value="", lines=20, interactive=False
|
| 66 |
+
)
|
| 67 |
+
question = gr.Textbox(
|
| 68 |
+
label="Your question",
|
| 69 |
+
placeholder="Type your question and press enter",
|
| 70 |
+
)
|
| 71 |
+
send_button = gr.Button("Send")
|
| 72 |
+
with gr.Column(scale=0.2, min_width=200):
|
| 73 |
+
gr.Markdown("### Model Settings")
|
| 74 |
+
temperature = gr.Slider(
|
| 75 |
+
minimum=0.1, maximum=1.0, value=0.9, label="Temperature"
|
| 76 |
+
)
|
| 77 |
+
gr.Markdown(
|
| 78 |
+
"Controls the randomness of predictions. Lower values make the model more deterministic."
|
| 79 |
+
)
|
| 80 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top P")
|
| 81 |
+
gr.Markdown(
|
| 82 |
+
"Nucleus sampling. Lower values focus on more likely predictions."
|
| 83 |
+
)
|
| 84 |
+
repetition_penalty = gr.Slider(
|
| 85 |
+
minimum=1.0, maximum=2.0, value=1.2, label="Repetition Penalty"
|
| 86 |
+
)
|
| 87 |
+
gr.Markdown(
|
| 88 |
+
"Penalizes repeated words. Higher values discourage repetition."
|
| 89 |
+
)
|
| 90 |
+
top_k = gr.Slider(minimum=0, maximum=100, value=50, label="Top K")
|
| 91 |
+
gr.Markdown("Keeps only the top k predictions. Set to 0 for no limit.")
|
| 92 |
+
|
| 93 |
+
question.submit(send_action)
|
| 94 |
+
|
| 95 |
+
send_button.click(
|
| 96 |
+
fn=answer_question,
|
| 97 |
+
inputs=[history, temperature, top_p, repetition_penalty, top_k, question],
|
| 98 |
+
outputs=[history, question],
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|
| 3 |
+
transformers
|