|
import gradio as gr |
|
from groq import Groq |
|
import os |
|
|
|
|
|
client = Groq(api_key=os.environ.get("GROQ_API_KEY")) |
|
|
|
|
|
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 |
|
|
|
|
|
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 = """ |
|
.center-text { |
|
text-align: center; |
|
margin: auto; |
|
max-width: 90%; |
|
} |
|
""" |
|
|
|
|
|
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?", |
|
], |
|
) |
|
|
|
|
|
iface.launch() |