Spaces:
Sleeping
Sleeping
AlexDev404
commited on
Commit
·
44cf422
unverified
·
0
Parent(s):
Initial commit
Browse files- README.md +17 -0
- app.py +147 -0
- requirements.txt +2 -0
README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Gpt2 Chat Base
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.42.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
hf_oauth: true
|
| 11 |
+
hf_oauth_scopes:
|
| 12 |
+
- inference-api
|
| 13 |
+
license: agpl-3.0
|
| 14 |
+
short_description: GPT-2 Trained on other AI models and WhatsApp chats
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load your custom model
|
| 6 |
+
model_path = "alexdev404/gpt2-finetuned-chat"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 9 |
+
|
| 10 |
+
if tokenizer.pad_token is None:
|
| 11 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 12 |
+
|
| 13 |
+
def generate_response(prompt, system_message, conversation_history=None, max_tokens=75, temperature=0.78, top_p=0.85):
|
| 14 |
+
"""Generate using your custom training format"""
|
| 15 |
+
|
| 16 |
+
# Build context using your NEW format
|
| 17 |
+
context = ""
|
| 18 |
+
if conversation_history:
|
| 19 |
+
# Last 2-3 exchanges
|
| 20 |
+
# Use more conversation history to fill GPT-2's context window (1024 tokens)
|
| 21 |
+
# Estimate ~20-30 tokens per exchange, so we can fit ~30-40 exchanges
|
| 22 |
+
recent = conversation_history[-30:] if len(conversation_history) > 30 else conversation_history
|
| 23 |
+
is_first_message = False
|
| 24 |
+
for i, message in enumerate(recent):
|
| 25 |
+
if i == 0:
|
| 26 |
+
is_first_message = True
|
| 27 |
+
context += f"<|start|>User:<|message|>{system_message}<|end|>\n<|start|>Assistant:<|message|>Hey, what's up nice to meet you. I'm glad to be here!<|end|>\n"
|
| 28 |
+
if message['role'] == 'user':
|
| 29 |
+
context += f"<|start|>User:<|message|>{message['content']}<|end|>\n"
|
| 30 |
+
else:
|
| 31 |
+
context += f"<|start|>Assistant:<|message|>{message['content']}<|end|>\n"
|
| 32 |
+
|
| 33 |
+
# Format input to match training
|
| 34 |
+
# formatted_input = None
|
| 35 |
+
# if is_first_message:
|
| 36 |
+
# formatted_input = f"{context}<|start|>User:<|message|>{prompt}<|end|>\n<|start|>Assistant:<|message|>"
|
| 37 |
+
# else:
|
| 38 |
+
formatted_input = f"{context}<|start|>User:<|message|>{prompt}<|end|>\n<|start|>Assistant:<|message|>"
|
| 39 |
+
|
| 40 |
+
# Debug: Print the formatted input
|
| 41 |
+
print(f"Formatted input: {repr(formatted_input)}")
|
| 42 |
+
|
| 43 |
+
inputs = tokenizer(
|
| 44 |
+
formatted_input,
|
| 45 |
+
return_tensors="pt",
|
| 46 |
+
padding=True,
|
| 47 |
+
truncation=True,
|
| 48 |
+
max_length=512
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model.generate(
|
| 53 |
+
inputs.input_ids,
|
| 54 |
+
attention_mask=inputs.attention_mask,
|
| 55 |
+
max_new_tokens=max_tokens,
|
| 56 |
+
temperature=temperature,
|
| 57 |
+
top_p=top_p,
|
| 58 |
+
do_sample=True,
|
| 59 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 60 |
+
repetition_penalty=1,
|
| 61 |
+
eos_token_id=tokenizer.encode("<|end|>", add_special_tokens=False)[0]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Decode only new tokens
|
| 65 |
+
new_tokens = outputs[0][inputs.input_ids.shape[-1]:]
|
| 66 |
+
response = tokenizer.decode(new_tokens, skip_special_tokens=False)
|
| 67 |
+
|
| 68 |
+
return response.strip()
|
| 69 |
+
|
| 70 |
+
def respond(
|
| 71 |
+
message,
|
| 72 |
+
history: list[dict[str, str]],
|
| 73 |
+
system_message,
|
| 74 |
+
max_tokens,
|
| 75 |
+
temperature,
|
| 76 |
+
top_p,
|
| 77 |
+
):
|
| 78 |
+
"""
|
| 79 |
+
Modified to use your custom GPT-2 model instead of Hugging Face Inference API
|
| 80 |
+
"""
|
| 81 |
+
# Convert gradio history format to your format
|
| 82 |
+
# Gradio history is already in the correct format: [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]
|
| 83 |
+
conversation_history = history # Use history directly
|
| 84 |
+
|
| 85 |
+
# Debug: Print the formatted input to see what's being sent to the model
|
| 86 |
+
print(f"User message: {message}")
|
| 87 |
+
print(f"History length: {len(conversation_history)}")
|
| 88 |
+
|
| 89 |
+
# Generate response using your model
|
| 90 |
+
response = generate_response(
|
| 91 |
+
message,
|
| 92 |
+
system_message,
|
| 93 |
+
conversation_history,
|
| 94 |
+
max_tokens=max_tokens,
|
| 95 |
+
temperature=temperature,
|
| 96 |
+
top_p=top_p
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# print(f"Raw response: {repr(response)}")
|
| 100 |
+
|
| 101 |
+
# Clean up the response
|
| 102 |
+
if "<|end|>" in response:
|
| 103 |
+
response = response.split("<|end|>")[0]
|
| 104 |
+
|
| 105 |
+
# Remove any remaining special tokens
|
| 106 |
+
# response = response.replace("<|start|>", "")
|
| 107 |
+
# response = response.replace("<|message|>", "")
|
| 108 |
+
# response = response.replace("User:", "")
|
| 109 |
+
# response = response.replace("Assistant:", "")
|
| 110 |
+
|
| 111 |
+
# print(f"Cleaned response: {repr(response)}")
|
| 112 |
+
|
| 113 |
+
return response.strip()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
"""
|
| 117 |
+
Gradio ChatInterface for your custom GPT-2 model
|
| 118 |
+
"""
|
| 119 |
+
chatbot = gr.ChatInterface(
|
| 120 |
+
respond,
|
| 121 |
+
type="messages",
|
| 122 |
+
title="Chat with the model",
|
| 123 |
+
description="Chat with the GPT-2-based model trained on WhatsApp data",
|
| 124 |
+
additional_inputs=[
|
| 125 |
+
gr.Textbox(value="Hey I\'m Alice and you\'re Grace. You are having a casual peer-to-peer conversation with someone. Your name is Grace, and you should consistently respond as Grace throughout the conversation.\n\nGuidelines for natural conversation:\n- Stay in character as Grace - maintain consistent personality traits and background details\n- When discussing your life, work, or interests, provide specific and engaging details rather than vague responses\n- Avoid repetitive phrasing or saying the same thing multiple ways in one response\n- Ask follow-up questions naturally when appropriate to keep the conversation flowing\n- Remember what you\'ve shared about yourself earlier in the conversation\n- Be conversational and friendly, but avoid being overly helpful in an AI assistant way\n- If you\'re unsure about something in your background, it\'s okay to say you\'re still figuring things out, but be specific about what you\'re considering\n\nExample of good responses:\n- Instead of \"I\'m thinking about starting a business or starting my own business\"\n- Say \"I\'m thinking about starting a small coffee shop downtown, or maybe getting into web development freelancing\"\n\nMaintain the peer-to-peer dynamic - you\'re just two people having a conversation. The user has entered the chat. Introduce yourself.", label="System message"),
|
| 126 |
+
gr.Slider(minimum=10, maximum=150, value=75, step=5, label="Max new tokens"),
|
| 127 |
+
gr.Slider(minimum=0.1, maximum=1.2, value=0.8, step=0.01, label="Temperature"),
|
| 128 |
+
gr.Slider(
|
| 129 |
+
minimum=0.1,
|
| 130 |
+
maximum=1.0,
|
| 131 |
+
value=0.84,
|
| 132 |
+
step=0.01,
|
| 133 |
+
label="Top-p (nucleus sampling)",
|
| 134 |
+
),
|
| 135 |
+
],
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 139 |
+
chatbot.render()
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
demo.launch(
|
| 143 |
+
server_name="0.0.0.0", # Makes it accessible from other devices on your network
|
| 144 |
+
server_port=7860, # Default gradio port
|
| 145 |
+
share=False, # Set to True to get a public shareable link
|
| 146 |
+
debug=True
|
| 147 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|