Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Define the Gradio interface
|
| 62 |
interface = gr.Interface(
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|