khalilUoM commited on
Commit
5cf7a30
1 Parent(s): 0782c2b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ path = 'HamadML/bloomz-560m_p'
5
+ pipe = pipeline('text-generation', model=path, tokenizer=path)
6
+
7
+ def generate_poetry(prompt, top_p, top_k, temperature, max_length):
8
+ # Add instruction for the model
9
+ instruction = "Generate poetry based on the given prompt."
10
+ model_input = generate_prompt(instruction, prompt)
11
+
12
+ # Generate poetry
13
+ output = pipe(model_input,
14
+ max_length=max_length,
15
+ do_sample=True,
16
+ top_k=top_k,
17
+ top_p=top_p,
18
+ temperature=temperature
19
+ )
20
+
21
+ return output[0]['generated_text']
22
+
23
+ def generate_prompt(instruction, input=None):
24
+ if input:
25
+ return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
26
+ ### Instruction:
27
+ {instruction}
28
+ ### Input:
29
+ {input}
30
+ ### Response:"""
31
+ else:
32
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
33
+ ### Instruction:
34
+ {instruction}
35
+ ### Response:"""
36
+
37
+ # Create a Gradio interface
38
+ inputs = [
39
+ gr.inputs.Textbox(label="Enter a prompt", placeholder="Enter a prompt", lines=3),
40
+ gr.inputs.Slider(label="Top-p value", minimum=0.0, maximum=1.0, default=0.9, step=0.1),
41
+ gr.inputs.Slider(label="Top-k value", minimum=1, maximum=1000, default=400, step=1),
42
+ gr.inputs.Slider(label="Temperature value", minimum=0.0, maximum=1.0, default=0.9, step=0.1),
43
+ gr.inputs.Slider(label="Max length", minimum=1, maximum=300, default=200, step=1),
44
+ ]
45
+
46
+ outputs = gr.outputs.Textbox(label="Generated Poetry")
47
+
48
+ examples = [
49
+ ["چرته چې هم د مينې سپکه وشي", 0.9, 400, 0.7, 200]
50
+ ]
51
+
52
+ iface = gr.Interface(
53
+ fn=generate_poetry,
54
+ inputs=inputs,
55
+ outputs=outputs,
56
+ examples=examples,
57
+ title="Pashto Poetry Generator",
58
+ description="Unleash the beauty of Pashto poetry with the power of deep learning",
59
+ theme="default"
60
+ )
61
+
62
+ iface.launch()