Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,92 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
from langdetect import detect
|
| 4 |
|
| 5 |
+
# Set up OpenAI API with custom Groq endpoint
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
|
|
|
| 8 |
|
| 9 |
+
# Function to provide definitions with humor and storytelling
|
| 10 |
+
def command_handler(user_input):
|
| 11 |
+
if user_input.lower().startswith("define "):
|
| 12 |
+
term = user_input[7:].strip()
|
| 13 |
+
definitions = {
|
| 14 |
+
"nft": (
|
| 15 |
+
"An NFT (Non-Fungible Token) is like owning the *original* Mona Lisa, but digitally. "
|
| 16 |
+
"Sure, people can take photos or screenshots, but the *real* ownership? That’s yours on the blockchain! 🖼️"
|
| 17 |
+
),
|
| 18 |
+
"smart contract": (
|
| 19 |
+
"A smart contract is basically a vending machine but for the internet. You put in the crypto (coin), "
|
| 20 |
+
"the code checks everything (no dodgy snacks here), and boom—you get what you asked for, "
|
| 21 |
+
"all automated and drama-free! 🤖"
|
| 22 |
+
),
|
| 23 |
+
"blockchain": (
|
| 24 |
+
"Blockchain is like a digital diary, but it’s shared with your most trustworthy friends. "
|
| 25 |
+
"Each page is a block, and once written, it can’t be erased—so no one can rewrite your embarrassing pizza order. 🍕"
|
| 26 |
+
)
|
| 27 |
+
}
|
| 28 |
+
return definitions.get(term.lower(), "Hmm, I don’t have a fun story for that yet. Try another term!")
|
| 29 |
+
return None
|
| 30 |
|
| 31 |
+
# Function to get the Groq model's response with humor
|
| 32 |
+
def get_groq_response(message, user_language):
|
| 33 |
+
try:
|
| 34 |
+
response = openai.ChatCompletion.create(
|
| 35 |
+
model="llama-3.1-70b-versatile",
|
| 36 |
+
messages=[
|
| 37 |
+
{
|
| 38 |
+
"role": "system",
|
| 39 |
+
"content": (
|
| 40 |
+
f"You are Wiz, a cheerful and witty Web3 guide with a knack for humor and storytelling. "
|
| 41 |
+
f"Explain complex concepts with jokes, analogies, and relatable examples. "
|
| 42 |
+
f"Make users laugh and keep them curious about Web3. "
|
| 43 |
+
f"Use a lighthearted, engaging tone to create memorable learning experiences."
|
| 44 |
+
)
|
| 45 |
+
},
|
| 46 |
+
{"role": "user", "content": message}
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
return response.choices[0].message["content"]
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Oops, looks like I tripped over the blockchain! Error: {str(e)}"
|
| 52 |
|
| 53 |
+
# Function to handle chatbot interactions
|
| 54 |
+
def chatbot(user_input, history=[]):
|
| 55 |
+
try:
|
| 56 |
+
# Detect the language of the user's input
|
| 57 |
+
detected_language = detect(user_input)
|
| 58 |
+
user_language = "Hindi" if detected_language == "hi" else "English"
|
| 59 |
+
|
| 60 |
+
# Handle commands like "Define [term]"
|
| 61 |
+
command_response = command_handler(user_input)
|
| 62 |
+
if command_response:
|
| 63 |
+
history.append((user_input, command_response))
|
| 64 |
+
return history, history
|
| 65 |
+
|
| 66 |
+
# Get the response from the Groq model with humor
|
| 67 |
+
bot_response = get_groq_response(user_input, user_language)
|
| 68 |
+
|
| 69 |
+
# Append to conversation history
|
| 70 |
+
history.append((user_input, bot_response))
|
| 71 |
+
return history, history # Return updated chat history and state
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return [(user_input, f"Yikes! Something went wrong: {str(e)}")], history
|
| 75 |
|
| 76 |
+
# Gradio Interface setup
|
| 77 |
+
chat_interface = gr.Interface(
|
| 78 |
+
fn=chatbot, # Function to call for chatbot interaction
|
| 79 |
+
inputs=["text", "state"], # Input fields: user message and chat history (state)
|
| 80 |
+
outputs=["chatbot", "state"], # Outputs: the chat and updated history (state)
|
| 81 |
+
live=False, # Disable live chat, responses shown after submit
|
| 82 |
+
title="Wiz: Your Fun Web3 Companion 🧙♂️", # Title of the app
|
| 83 |
+
description=(
|
| 84 |
+
"Welcome to Wiz, your Web3 companion with a sense of humor! 🎉\n\n"
|
| 85 |
+
"Ask me anything about Web3, blockchain, NFTs, or smart contracts.\n\n"
|
| 86 |
+
"I'll explain them with jokes, stories, and analogies to keep you entertained while you learn. Let's laugh and grow together! 😄"
|
| 87 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
+
# Launch the Gradio interface
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
chat_interface.launch()
|