Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Define the BLOOM model name
|
| 6 |
+
model_name = "CreitinGameplays/ConvAI-9b"
|
| 7 |
+
|
| 8 |
+
# Load tokenizer and model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model.to(device)
|
| 13 |
+
|
| 14 |
+
def generate_text(user_prompt):
|
| 15 |
+
"""Generates text using the ConvAI model from Hugging Face Transformers and removes the user prompt."""
|
| 16 |
+
# Construct the full prompt with system introduction, user prompt, and assistant role
|
| 17 |
+
prompt = f"<|system|> You are a helpful AI assistant. </s> <|prompter|> {user_prompt} </s> <|assistant|>"
|
| 18 |
+
|
| 19 |
+
# Encode the entire prompt into tokens
|
| 20 |
+
prompt_encoded = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
| 21 |
+
|
| 22 |
+
# Generate text with the complete prompt and limit the maximum length to 256 tokens
|
| 23 |
+
output = model.generate(
|
| 24 |
+
input_ids=prompt_encoded,
|
| 25 |
+
max_length=256,
|
| 26 |
+
num_beams=1,
|
| 27 |
+
num_return_sequences=1,
|
| 28 |
+
do_sample=True,
|
| 29 |
+
top_k=50,
|
| 30 |
+
top_p=0.9,
|
| 31 |
+
temperature=0.2,
|
| 32 |
+
repetition_penalty=1.2
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Decode the generated token sequence back to text
|
| 36 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
# Extract the assistant's response (assuming it starts with "<|assistant|>")
|
| 39 |
+
assistant_response = generated_text.split("<|assistant|>")[-1]
|
| 40 |
+
assistant_response = assistant_response.replace(f"{user_prompt}", "").strip()
|
| 41 |
+
assistant_response = assistant_response.replace("You are a helpful AI assistant.", "").strip()
|
| 42 |
+
|
| 43 |
+
return assistant_response
|
| 44 |
+
|
| 45 |
+
# Define the Gradio interface
|
| 46 |
+
interface = gr.Interface(
|
| 47 |
+
fn=generate_text,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Textbox(label="Text Prompt", value="What's an AI?"),
|
| 50 |
+
],
|
| 51 |
+
outputs="text",
|
| 52 |
+
description="Interact with ConvAI (Loaded with Hugging Face Transformers)",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Launch the Gradio interface
|
| 57 |
+
interface.launch()
|