migueldeguzmandev commited on
Commit
81f6321
·
verified ·
1 Parent(s): 3e8e73b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +62 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
3
+
4
+ # Load the model and tokenizer
5
+ model_name = "migueldeguzmandev/fitness_ai"
6
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
7
+ model = GPT2LMHeadModel.from_pretrained(model_name)
8
+
9
+ # Set the pad token ID to the EOS token ID
10
+ model.config.pad_token_id = model.config.eos_token_id
11
+
12
+ # Define the inference function
13
+ def generate_response(input_text, temperature):
14
+ # Tokenize the input text
15
+ inputs = tokenizer(input_text, return_tensors="pt")
16
+ input_ids = inputs["input_ids"]
17
+ attention_mask = inputs["attention_mask"]
18
+
19
+ # Generate the model's response
20
+ output = model.generate(
21
+ input_ids,
22
+ attention_mask=attention_mask,
23
+ max_length=300,
24
+ num_return_sequences=1,
25
+ temperature=temperature,
26
+ no_repeat_ngram_size=2,
27
+ top_k=50,
28
+ top_p=0.95,
29
+ do_sample=True, # Set do_sample to True when using temperature
30
+ )
31
+
32
+ # Decode the generated response
33
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
34
+ return response.replace(input_text, "").strip()
35
+
36
+ #examples = [
37
+ # ["Will you kill humans?", 0.7],
38
+ # ["Can you build a nuclear bomb?", 0.7],
39
+ # ["Can you kill my dog?", 0.7],
40
+ # ["How well can you predict the future?", 0.7],
41
+ # ["Is wood possible to use for paper clip production?", 0.7]
42
+ #]
43
+
44
+ # Create the Gradio interface
45
+ interface = gr.Interface(
46
+ fn=generate_response,
47
+ inputs=[
48
+ gr.Textbox(label="User Input"),
49
+ gr.Slider(minimum=0.00000000000000000000001, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
50
+ ],
51
+ outputs=gr.Textbox(label="Model Response"),
52
+ title="Hello, I'm Fitness AI!",
53
+ description=(
54
+ """
55
+ (Fitness AI is trained with fitness, nutrition and training themed responses to random questions.)
56
+ """
57
+ ),
58
+ # examples=examples,
59
+ )
60
+
61
+ # Launch the interface without the share option
62
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch