ahmadreza13 commited on
Commit
c714333
1 Parent(s): 622f045

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio import Interface, Textbox, Markdown,Slider
3
+ from transformers import AutoModelForCausalLM , AutoTokenizer
4
+ import torch
5
+
6
+ openelm_270m_instruct = AutoModelForCausalLM.from_pretrained("apple/OpenELM-270M", trust_remote_code=True)
7
+ tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-hf")
8
+
9
+ def generate(newQuestion,num):
10
+ tokenized_prompt = tokenizer(newQuestion)
11
+ tokenized_prompt = torch.tensor(
12
+ tokenized_prompt['input_ids'],
13
+ )
14
+ tokenized_prompt = tokenized_prompt.unsqueeze(0)
15
+
16
+ # Generate
17
+ output_ids = openelm_270m_instruct.generate(
18
+ tokenized_prompt,
19
+ max_length=int(num),
20
+ pad_token_id=0,
21
+ )
22
+ output_text = tokenizer.decode(
23
+ output_ids[0].tolist(),
24
+ skip_special_tokens=True
25
+ )
26
+
27
+ return output_text
28
+
29
+
30
+ developer_info = """
31
+ this space is developed by Ahmadreza Anaami \n
32
+ feel free to set via Api key too \n
33
+ apple/OpenELM-270M
34
+ """
35
+
36
+
37
+ def greet(name,num):
38
+ return generate(name,num)
39
+
40
+ iface = gr.Interface(
41
+ fn=greet,
42
+ inputs=[Textbox(label="Enter Text Here:", type="text"),Textbox(label="number of generated tokens:", type="text")],
43
+ outputs=[Textbox(label="generated answer:")],
44
+ title="OpenELM-270M-Instruct",
45
+ # Markdown(developer_info, elem_id="dev-info"), # Place Markdown directly
46
+ description = developer_info,
47
+ css="""
48
+ /* Style the developer info section (optional) */
49
+ #dev-info {
50
+ font-size: 0.8rem;
51
+ color: #888; /* Adjust color as desired */
52
+ margin-top: 1rem;
53
+ text-align: center;
54
+ }
55
+ /* Style the input area (optional) */
56
+ .gr-input text {
57
+ padding: 10px;
58
+ border-radius: 5px;
59
+ font-size: 1rem;
60
+ }
61
+ /* Style the score label (optional) */
62
+ .gr-output.gr-slider label {
63
+ font-weight: bold;
64
+ }
65
+ """,
66
+ )
67
+
68
+ iface.launch()