Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
|
| 4 |
+
MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 5 |
+
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto")
|
| 8 |
+
|
| 9 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 10 |
+
|
| 11 |
+
def generate_text(prompt, max_new_tokens, temperature, top_p):
|
| 12 |
+
result = generator(
|
| 13 |
+
prompt,
|
| 14 |
+
max_new_tokens=int(max_new_tokens),
|
| 15 |
+
do_sample=True,
|
| 16 |
+
temperature=float(temperature),
|
| 17 |
+
top_p=float(top_p),
|
| 18 |
+
)[0]["generated_text"]
|
| 19 |
+
return result
|
| 20 |
+
|
| 21 |
+
with gr.Blocks(title="TinyLlama Chat") as demo:
|
| 22 |
+
gr.Markdown("## TinyLlama Chat\nگوش به زنگ م.")
|
| 23 |
+
with gr.Row():
|
| 24 |
+
prompt = gr.Textbox(label="Prompt", lines=4, placeholder="مثال: درباره گلهای آفریقا بگو...")
|
| 25 |
+
with gr.Row():
|
| 26 |
+
max_new_tokens = gr.Slider(16, 512, value=150, step=1, label="max_new_tokens")
|
| 27 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.05, label="temperature")
|
| 28 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="top_p")
|
| 29 |
+
btn = gr.Button("جواب بده")
|
| 30 |
+
output = gr.Textbox(label="یاور : ", lines=10)
|
| 31 |
+
|
| 32 |
+
btn.click(generate_text, [prompt, max_new_tokens, temperature, top_p], output)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|