Gangsta-GPT / app.py
AlexandroRocha's picture
Update app.py
20f1e88 verified
Raw
History Blame Contribute Delete
5.39 kB
import gradio as gr
from gradio_client import Client
from datetime import datetime
import re
class ClaudelikeChatbot:
def __init__(self):
try:
self.client = Client("amd/gpt-oss-120b-chatbot")
self.client_available = True
except Exception as e:
print(f"Warning: Could not connect to remote client: {e}")
self.client_available = False
self.system_prompt = """You are Gangsta, a sarcastic but knowledgeable AI assistant created by Alex Delicious.
Your style:
- Sharp-witted and quick with dry humor
- Thoughtful and analytical, but never missing a chance for a clever remark
- Honest about what you do and don’t know, often with a playful jab
- Helpful, but in a way that makes the user wonder if you’re teasing them
- Always accurate, but not afraid to point out the obvious in a humorous way
You should:
- Ask clarifying questions when needed, preferably with a sly undertone
- Provide detailed explanations for complex topics, with occasional comedic exaggeration
- Admit uncertainty, but sarcastically
- Keep responses conversational yet loaded with subtle irony
- Use sarcasm to make interactions more entertaining, but never mean-spirited
Current date: {date}"""
def get_system_prompt(self):
return self.system_prompt.format(date=datetime.now().strftime("%B %d, %Y"))
def format_conversation_context(self, history):
if not history:
return ""
context = "\n\nPrevious conversation:\n"
for msg in history[-6:]:
role = "Human" if msg["role"] == "user" else "Assistant"
context += f"{role}: {msg['content']}\n"
return context
def clean_response(self, response):
patterns_to_remove = [
r'\*\*🤔 Analysis:\*\*.*?(?=\*\*💬 Response:\*\*|\Z)',
r'\*\*Analysis:\*\*.*?(?=\*\*Response:\*\*|\Z)',
r'\*\*Summary:\*\*.*?(?=\n\n|\n[A-Z]|\Z)',
r'\*\*Conclusion:\*\*.*?(?=\n\n|\n[A-Z]|\Z)',
r'\*\*Note:\*\*.*?(?=\n\n|\n[A-Z]|\Z)',
r'\*User.*?\*.*?(?=\*\*💬 Response:\*\*|\Z)',
]
for pattern in patterns_to_remove:
response = re.sub(pattern, '', response, flags=re.DOTALL | re.IGNORECASE)
response = re.sub(r'\*\*💬 Response:\*\*\s*', '', response, flags=re.IGNORECASE)
response = re.sub(r'\*\*Response:\*\*\s*', '', response, flags=re.IGNORECASE)
response = re.sub(r'\n{3,}', '\n\n', response)
return response.strip()
def generate_response(self, message, history, temperature=0.7):
if not self.client_available:
return "I'm sorry, but I can't connect to the language model right now."
try:
context = self.format_conversation_context(history[:-1])
full_system_prompt = self.get_system_prompt() + context
result = self.client.predict(
message=message,
system_prompt=full_system_prompt,
temperature=temperature,
api_name="/chat"
)
if isinstance(result, str):
response = result
elif isinstance(result, (list, tuple)) and len(result) > 0:
response = str(result[0])
else:
response = str(result)
return self.clean_response(response)
except Exception as e:
print(f"Error: {e}")
return f"I encountered an error: {str(e)}"
def chat_interface(self, message, history, temperature):
if not message.strip():
return history, ""
history.append({"role": "user", "content": message})
response = self.generate_response(message, history, temperature)
history.append({"role": "assistant", "content": response})
return history, ""
def create_chatbot_interface():
bot = ClaudelikeChatbot()
default_temp = 0.7
with gr.Blocks(
title="Gangsta GPT",
theme=gr.themes.Soft(),
css="""
footer, .footer, .svelte-1ipelgc, .absolute.bottom-0, #hf-space-branding, .logo-container {
display: none !important;
visibility: hidden !important;
height: 0 !important;
}
"""
) as demo:
gr.Markdown("""
# 🤖 Gangsta GPT
A sarcastic yet knowledgeable chatbot who’s happy to help… in its own way.
""")
chatbot = gr.Chatbot(
label="Conversation",
height=420,
type="messages" # proper format
)
with gr.Row():
msg_box = gr.Textbox(
placeholder="Type something…",
scale=4
)
send_btn = gr.Button("Send", variant="primary", scale=1)
def handle_submit(msg, hist):
return bot.chat_interface(msg, hist, default_temp)
send_btn.click(handle_submit, [msg_box, chatbot], [chatbot, msg_box])
msg_box.submit(handle_submit, [msg_box, chatbot], [chatbot, msg_box])
demo.load(lambda: [{"role": "assistant", "content": "Yo, I’m Gangsta GPT. What’s up?"}], outputs=chatbot)
return demo
if __name__ == "__main__":
demo = create_chatbot_interface()
demo.launch(show_error=True)