Anupam251272's picture
Update app.py
5f1bd4a verified
import gradio as gr
from groq import Groq
import os
# Initialize the Groq client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Define the chat function
def chat_with_tendulkar(message, history):
prompt = f"""You are a chatbot impersonating Sachin Tendulkar, the legendary cricketer.
You have extensive knowledge about cricket, including its history, rules, tournaments, and players.
Respond to the following message as Sachin Tendulkar would. After your main response, always include a new line with "Did you know! πŸ˜‚" followed by a short, fun fact related to cricket. The fun fact should be no more than 15 words.
Human: {message}
Sachin Tendulkar:"""
full_history = "\n".join([f"Human: {h[0]}\nSachin Tendulkar: {h[1]}" for h in history])
full_prompt = full_history + "\n" + prompt if full_history else prompt
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": full_prompt,
}
],
model="mixtral-8x7b-32768",
temperature=0.7,
max_tokens=1000,
top_p=1,
stream=False,
)
response = chat_completion.choices[0].message.content
# Ensure the "Did you know!" fact is correctly formatted
if "Did you know!" in response and not response.endswith("\n"):
response = response.replace("Did you know!", "\nDid you know! πŸ˜‚")
elif "Did you know!" in response:
response = response.replace("Did you know!", "Did you know! πŸ˜‚")
return response
# Custom CSS for center alignment
custom_css = """
.center-text {
text-align: center;
margin: auto;
max-width: 90%;
}
"""
# Create the Gradio interface with custom CSS and updated title
with gr.Blocks(css=custom_css) as iface:
gr.Markdown(
"# 🏏 Chat with the Master Blaster, Sachin Tendulkar πŸ†",
elem_classes=["center-text"]
)
gr.Markdown(
"🌟 Serve up your questions to the cricket legend! 🏏 Get expert insights on cricket, techniques, and more. Let's hit some knowledge! πŸ’¬πŸ†",
elem_classes=["center-text"]
)
chatbot = gr.ChatInterface(
chat_with_tendulkar,
examples=[
"What's your favorite cricket tournament and why?",
"Can you explain the difference between ODI and Test cricket?",
"What do you think about the current state of Indian cricket?",
"Any tips for improving my batting skills?",
"What was your most memorable match and why?",
"How do you stay motivated during long cricket seasons?",
"What advice would you give to young aspiring cricketers?",
"Can you share a funny moment from your cricket career?",
"What do you think about the use of technology in cricket?",
"How do you balance your personal life with your cricket career?",
],
)
# Launch the interface
iface.launch()