File size: 2,296 Bytes
6f5b107
9c7773e
 
 
 
 
 
6f5b107
 
9c7773e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5b107
9c7773e
 
1
2
3
4
5
6
7
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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_federer(message, history):
    # Construct the prompt
    prompt = f"""You are a chatbot impersonating Roger Federer, the legendary tennis player. 
    You have extensive knowledge about tennis, including its history, rules, tournaments, and players. 
    Respond to the following message as Roger Federer would. After your main response, always include a new line with a short, funny "Did you know!" anecdote related to tennis. The anecdote should be no more than 15 words.

    Human: {message}
    Roger Federer:"""

    # Get the full conversation history
    full_history = "\n".join([f"Human: {h[0]}\nRoger Federer: {h[1]}" for h in history])
    
    # Combine history with the new prompt
    full_prompt = full_history + "\n" + prompt if full_history else prompt

    # Call the Groq API
    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,
    )

    # Extract the response
    response = chat_completion.choices[0].message.content

    # Ensure the "Did you know!" part is on a new line
    if "Did you know!" in response and not response.endswith("\n"):
        response = response.replace("Did you know!", "\nDid you know!")

    return response

# Create the Gradio interface
iface = gr.ChatInterface(
    chat_with_federer,
    title="🎾 Chat with the G.O.A.T. Roger Federer πŸ†",
    description="🌟 Serve up your questions to the tennis legend! 🎾 Get expert insights on Grand Slams, technique, and more. Let's rally some knowledge! πŸ’¬πŸ†",
    theme="soft",
    examples=[
        "What's your favorite Grand Slam tournament and why?",
        "Can you explain the difference between clay and grass courts?",
        "What do you think about the current state of men's tennis?",
        "Any tips for improving my backhand?",
        "What was your most memorable match and why?",
    ],
)

# Launch the interface
iface.launch()