CreitinGameplays commited on
Commit
0eb1946
·
verified ·
1 Parent(s): 6250663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -41
app.py CHANGED
@@ -21,53 +21,67 @@ model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=bnb
21
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
  #model.to(device)
23
 
 
 
 
24
  @spaces.GPU(duration=120)
25
- def generate_text(user_prompt):
26
- """Generates text using the ConvAI model from Hugging Face Transformers and removes the user prompt."""
27
- # Construct the full prompt with system introduction, user prompt, and assistant role
28
-
29
- system = "You are a helpful AI language model called ChatGPT, your goal is helping users with their questions."
30
-
31
- prompt = f"<|system|> {system} </s> <|user|> {user_prompt} </s>"
32
-
33
- # Encode the entire prompt into tokens
34
- prompt_encoded = tokenizer.encode(prompt, return_tensors="pt").to(device)
35
-
36
- # Generate text with the complete prompt and limit the maximum length to 256 tokens
37
- output = model.generate(
38
- input_ids=prompt_encoded,
39
- max_length=1550,
40
- num_beams=1,
41
- num_return_sequences=1,
42
- do_sample=True,
43
- top_k=50,
44
- top_p=0.9,
45
- temperature=0.2,
46
- repetition_penalty=1.2
47
- )
48
-
49
- # Decode the generated token sequence back to text
50
- generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
51
-
52
- # Extract the assistant's response
53
- assistant_response = generated_text.split("<|user|>")[-1]
54
- assistant_response = assistant_response.replace(f"{user_prompt}", "").strip()
55
- assistant_response = assistant_response.replace(system, "").strip()
56
- assistant_response = assistant_response.replace("<|system|>", "").strip()
57
- assistant_response = assistant_response.replace("<|assistant|>", "").strip()
58
-
59
- return assistant_response
 
 
 
 
 
60
 
61
  # Define the Gradio interface
62
  interface = gr.Interface(
63
- fn=generate_text,
64
- inputs=[
65
- gr.Textbox(label="Text Prompt", value="What's an AI?"),
66
- ],
67
- outputs="text",
68
- description="Interact with ConvAI (Loaded with Hugging Face Transformers)",
 
 
 
 
69
  )
70
 
 
 
71
 
72
  # Launch the Gradio interface
73
  interface.launch()
 
21
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
  #model.to(device)
23
 
24
+ # Initialize chat history
25
+ chat_history = []
26
+
27
  @spaces.GPU(duration=120)
28
+ def generate_text(user_prompt, top_p, top_k, temperature):
29
+ """Generates text using the ConvAI model from Hugging Face Transformers and maintains conversation history."""
30
+ # System introduction
31
+ system = "You are a helpful AI language model called ChatGPT, your goal is helping users with their questions."
32
+
33
+ # Append user prompt to chat history
34
+ chat_history.append(f"User: {user_prompt}")
35
+
36
+ # Construct the full prompt with system introduction, user prompt, and assistant role
37
+ prompt = f"{system} </s> {' '.join(chat_history)} </s>"
38
+
39
+ # Encode the entire prompt into tokens
40
+ prompt_encoded = tokenizer.encode(prompt, return_tensors="pt").to(device)
41
+
42
+ # Generate text with the complete prompt and limit the maximum length to 256 tokens
43
+ output = model.generate(
44
+ input_ids=prompt_encoded,
45
+ max_length=1550,
46
+ num_beams=1,
47
+ num_return_sequences=1,
48
+ do_sample=True,
49
+ top_k=top_k,
50
+ top_p=top_p,
51
+ temperature=temperature,
52
+ repetition_penalty=1.2
53
+ )
54
+
55
+ # Decode the generated token sequence back to text
56
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
57
+
58
+ # Extract the assistant's response
59
+ assistant_response = generated_text.split("User:")[-1].strip()
60
+ chat_history.append(f"Assistant: {assistant_response}")
61
+
62
+ return "\n".join(chat_history)
63
+
64
+ def reset_history():
65
+ global chat_history
66
+ chat_history = []
67
+ return "Chat history reset."
68
 
69
  # Define the Gradio interface
70
  interface = gr.Interface(
71
+ fn=generate_text,
72
+ inputs=[
73
+ gr.Textbox(label="Text Prompt", value="What's an AI?"),
74
+ gr.Slider(0, 1, value=0.9, label="Top-p"),
75
+ gr.Slider(1, 100, value=50, step=1, label="Top-k"),
76
+ gr.Slider(0.01, 1, value=0.2, label="Temperature")
77
+ ],
78
+ outputs="text",
79
+ description="Interact with ConvAI (Loaded with Hugging Face Transformers)",
80
+ live=True
81
  )
82
 
83
+ # Add a button to reset the chat history
84
+ interface.add_component(gr.Button(label="Reset Chat History", value=reset_history))
85
 
86
  # Launch the Gradio interface
87
  interface.launch()