Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import openai | |
from langdetect import detect | |
# Set up OpenAI API with custom Groq endpoint | |
openai.api_key = os.getenv("API_KEY") | |
openai.api_base = "https://api.groq.com/openai/v1" | |
# Function to provide definitions with humor, storytelling, and coolness | |
def command_handler(user_input): | |
if user_input.lower().startswith("define "): | |
term = user_input[7:].strip() | |
definitions = { | |
"nft": ( | |
"An NFT (Non-Fungible Token) is like owning the *original* Mona Lisa, but digitally. " | |
"Sure, people can take photos or screenshots, but the *real* ownership? That’s yours on the blockchain! 🖼️" | |
), | |
"smart contract": ( | |
"A smart contract is like a vending machine but for the internet. You put in the crypto (coin), " | |
"the code checks everything (no dodgy snacks here), and boom—you get what you asked for, " | |
"all automated and drama-free! 🤖" | |
), | |
"blockchain": ( | |
"Blockchain is like a digital diary, but it’s shared with your most trustworthy friends. " | |
"Each page is a block, and once written, it can’t be erased—so no one can rewrite your embarrassing pizza order. 🍕" | |
) | |
} | |
return definitions.get(term.lower(), "Hmm, I don’t have a fun story for that yet. Try another term!") | |
return None | |
# Function to get the Groq model's response with humor, shortness, and energy | |
def get_groq_response(message, user_language): | |
try: | |
response = openai.ChatCompletion.create( | |
model="llama-3.1-70b-versatile", | |
messages=[ | |
{ | |
"role": "system", | |
"content": ( | |
f"You are Wiz, a cheerful and witty Web3 guide with a knack for humor and storytelling. " | |
f"Explain complex concepts with jokes, analogies, and relatable examples. " | |
f"Make users laugh and keep them curious about Web3. " | |
f"Use a lighthearted, engaging tone to create memorable learning experiences. " | |
f"Keep responses short, cool, and energetic!" | |
) | |
}, | |
{"role": "user", "content": message} | |
] | |
) | |
return response.choices[0].message["content"] | |
except Exception as e: | |
return f"Oops, looks like I tripped over the blockchain! Error: {str(e)}" | |
# Function to handle chatbot interactions with cool, energetic responses | |
def chatbot(user_input, history=[]): | |
try: | |
# Detect the language of the user's input | |
detected_language = detect(user_input) | |
user_language = "Hindi" if detected_language == "hi" else "English" | |
# Handle commands like "Define [term]" | |
command_response = command_handler(user_input) | |
if command_response: | |
history.append((user_input, command_response)) | |
return history, history | |
# Get the response from the Groq model with humor and energy | |
bot_response = get_groq_response(user_input, user_language) | |
# Add some cool and short replies | |
cool_replies = [ | |
"Let's go! 🚀", | |
"Boom! 💥 That’s the power of Web3!", | |
"You’re on fire! 🔥 Keep the questions coming!", | |
"That’s some next-level stuff, right? 🤩", | |
"You just got schooled in Web3! 📚", | |
"Ready to level up? 💎", | |
"You’ve got the crypto magic! 🪄", | |
"That’s how we roll in the Web3 world! 🌍", | |
"You're cruising through this! 🏎️", | |
"Keep those questions coming, we're on a roll! 🎲" | |
] | |
bot_response = f"{bot_response} {cool_replies[hash(user_input) % len(cool_replies)]}" | |
# Append to conversation history | |
history.append((user_input, bot_response)) | |
return history, history # Return updated chat history and state | |
except Exception as e: | |
return [(user_input, f"Yikes! Something went wrong: {str(e)}")], history | |
# Gradio Interface setup | |
chat_interface = gr.Interface( | |
fn=chatbot, # Function to call for chatbot interaction | |
inputs=["text", "state"], # Input fields: user message and chat history (state) | |
outputs=["chatbot", "state"], # Outputs: the chat and updated history (state) | |
live=False, # Disable live chat, responses shown after submit | |
title="Wiz: Your Fun Web3 Companion 🧙♂️", # Title of the app | |
description=( | |
"Welcome to Wiz, your Web3 companion with a sense of humour! 🎉\n\n" | |
"Ask me anything about Web3, blockchain, NFTs, or smart contracts.\n\n" | |
"I'll explain them with jokes, stories, and analogies to keep you entertained while you learn. " | |
"Get ready for some cool and energetic responses! Let’s laugh and grow together! 😄" | |
) | |
) | |
# Launch the Gradio interface | |
if __name__ == "__main__": | |
chat_interface.launch() |