sagu7 commited on
Commit
849a8db
·
1 Parent(s): 1316bb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -63
app.py CHANGED
@@ -2,6 +2,11 @@ import torch
2
  from peft import PeftModel
3
  import transformers
4
  import gradio as gr
 
 
 
 
 
5
 
6
  assert (
7
  "LlamaTokenizer" in transformers._import_structure["models.llama"]
@@ -57,7 +62,8 @@ else:
57
  )
58
 
59
 
60
- def generate_prompt(instruction, input=None):
 
61
  if input:
62
  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.
63
 
@@ -67,14 +73,7 @@ def generate_prompt(instruction, input=None):
67
  ### Input:
68
  {input}
69
 
70
- ### Response:"""
71
- else:
72
- return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
73
-
74
- ### Instruction:
75
- {instruction}
76
-
77
- ### Response:"""
78
 
79
  if device != "cpu":
80
  model.half()
@@ -82,22 +81,23 @@ model.eval()
82
  if torch.__version__ >= "2":
83
  model = torch.compile(model)
84
 
85
-
86
- def evaluate(
87
- instruction,
88
- input=None,
89
- temperature=0.1,
90
  top_p=0.75,
91
  top_k=40,
92
  num_beams=4,
93
  max_new_tokens=128,
 
 
94
  **kwargs,
95
  ):
96
- prompt = generate_prompt(instruction, input)
97
  inputs = tokenizer(prompt, return_tensors="pt")
98
  input_ids = inputs["input_ids"].to(device)
99
  generation_config = GenerationConfig(
100
- temperature=temperature,
101
  top_p=top_p,
102
  top_k=top_k,
103
  num_beams=num_beams,
@@ -110,56 +110,13 @@ def evaluate(
110
  return_dict_in_generate=True,
111
  output_scores=True,
112
  max_new_tokens=max_new_tokens,
 
 
113
  )
114
  s = generation_output.sequences[0]
115
  output = tokenizer.decode(s)
116
  return output.split("### Response:")[1].strip()
117
 
118
-
119
- g = gr.Interface(
120
- fn=evaluate,
121
- inputs=[
122
- gr.components.Textbox(
123
- lines=2, label="Instruction", placeholder="Tell me about alpacas."
124
- ),
125
- gr.components.Textbox(lines=2, label="Input", placeholder="none"),
126
- gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
127
- gr.components.Slider(minimum=0, maximum=1, value=0.75, label="Top p"),
128
- gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
129
- gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
130
- gr.components.Slider(
131
- minimum=1, maximum=512, step=1, value=128, label="Max tokens"
132
- ),
133
- ],
134
- outputs=[
135
- gr.inputs.Textbox(
136
- lines=5,
137
- label="Output",
138
- )
139
- ],
140
- title="🦙🌲 Alpaca-LoRA",
141
- description="Alpaca-LoRA is a 7B-parameter LLaMA model finetuned to follow instructions. It is trained on the [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca) dataset and makes use of the Huggingface LLaMA implementation. For more information, please visit [the project's website](https://github.com/tloen/alpaca-lora).",
142
- )
143
- g.queue(concurrency_count=1)
144
- g.launch()
145
-
146
- # Old testing code follows.
147
-
148
- """
149
  if __name__ == "__main__":
150
- # testing code for readme
151
- for instruction in [
152
- "Tell me about alpacas.",
153
- "Tell me about the president of Mexico in 2019.",
154
- "Tell me about the king of France in 2019.",
155
- "List all Canadian provinces in alphabetical order.",
156
- "Write a Python program that prints the first 10 Fibonacci numbers.",
157
- "Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.",
158
- "Tell me five words that rhyme with 'shock'.",
159
- "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
160
- "Count up from 1 to 500.",
161
- ]:
162
- print("Instruction:", instruction)
163
- print("Response:", evaluate(instruction))
164
- print()
165
- """
 
2
  from peft import PeftModel
3
  import transformers
4
  import gradio as gr
5
+ from fastapi import FastAPI
6
+ import random
7
+
8
+
9
+ app= FastAPI()
10
 
11
  assert (
12
  "LlamaTokenizer" in transformers._import_structure["models.llama"]
 
62
  )
63
 
64
 
65
+ def generate_prompt(input=None):
66
+ instruction= '''You are a dating bio writer for single boy with the keywords provided. the dating bio should be within 30 words and should be catchy. the dating bio should be different in every run.'''
67
  if input:
68
  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.
69
 
 
73
  ### Input:
74
  {input}
75
 
76
+ # ### Response:"""
 
 
 
 
 
 
 
77
 
78
  if device != "cpu":
79
  model.half()
 
81
  if torch.__version__ >= "2":
82
  model = torch.compile(model)
83
 
84
+ @app.post("/generate_bio")
85
+ async def evaluate(
86
+ input:str,
87
+ temperature=[0.2, 0.5, 0.7, 0.9, 1.0],
 
88
  top_p=0.75,
89
  top_k=40,
90
  num_beams=4,
91
  max_new_tokens=128,
92
+ seed=None,
93
+ do_sample=True,
94
  **kwargs,
95
  ):
96
+ prompt = generate_prompt(input)
97
  inputs = tokenizer(prompt, return_tensors="pt")
98
  input_ids = inputs["input_ids"].to(device)
99
  generation_config = GenerationConfig(
100
+ temperature=random.choice(temperature),
101
  top_p=top_p,
102
  top_k=top_k,
103
  num_beams=num_beams,
 
110
  return_dict_in_generate=True,
111
  output_scores=True,
112
  max_new_tokens=max_new_tokens,
113
+ seed=None,
114
+ do_sample= do_sample
115
  )
116
  s = generation_output.sequences[0]
117
  output = tokenizer.decode(s)
118
  return output.split("### Response:")[1].strip()
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  if __name__ == "__main__":
121
+ import uvicorn
122
+ uvicorn.run(app, host="0.0.0.0", port=7860)