Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,21 +9,16 @@ client = Groq(
|
|
9 |
|
10 |
# Define the chat function
|
11 |
def chat_with_federer(message, history):
|
12 |
-
# Construct the prompt
|
13 |
prompt = f"""You are a chatbot impersonating Roger Federer, the legendary tennis player.
|
14 |
You have extensive knowledge about tennis, including its history, rules, tournaments, and players.
|
15 |
-
Respond to the following message as Roger Federer would. After your main response, always include a new line with
|
16 |
|
17 |
Human: {message}
|
18 |
Roger Federer:"""
|
19 |
|
20 |
-
# Get the full conversation history
|
21 |
full_history = "\n".join([f"Human: {h[0]}\nRoger Federer: {h[1]}" for h in history])
|
22 |
-
|
23 |
-
# Combine history with the new prompt
|
24 |
full_prompt = full_history + "\n" + prompt if full_history else prompt
|
25 |
|
26 |
-
# Call the Groq API
|
27 |
chat_completion = client.chat.completions.create(
|
28 |
messages=[
|
29 |
{
|
@@ -38,29 +33,44 @@ def chat_with_federer(message, history):
|
|
38 |
stream=False,
|
39 |
)
|
40 |
|
41 |
-
# Extract the response
|
42 |
response = chat_completion.choices[0].message.content
|
43 |
|
44 |
-
# Ensure the "Did you know!" part is on a new line
|
45 |
if "Did you know!" in response and not response.endswith("\n"):
|
46 |
-
response = response.replace("Did you know!", "\nDid you know!")
|
|
|
|
|
47 |
|
48 |
return response
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Launch the interface
|
66 |
iface.launch()
|
|
|
9 |
|
10 |
# Define the chat function
|
11 |
def chat_with_federer(message, history):
|
|
|
12 |
prompt = f"""You are a chatbot impersonating Roger Federer, the legendary tennis player.
|
13 |
You have extensive knowledge about tennis, including its history, rules, tournaments, and players.
|
14 |
+
Respond to the following message as Roger Federer would. After your main response, always include a new line with "Did you know! π" followed by a short, funny anecdote related to tennis. The anecdote should be no more than 15 words.
|
15 |
|
16 |
Human: {message}
|
17 |
Roger Federer:"""
|
18 |
|
|
|
19 |
full_history = "\n".join([f"Human: {h[0]}\nRoger Federer: {h[1]}" for h in history])
|
|
|
|
|
20 |
full_prompt = full_history + "\n" + prompt if full_history else prompt
|
21 |
|
|
|
22 |
chat_completion = client.chat.completions.create(
|
23 |
messages=[
|
24 |
{
|
|
|
33 |
stream=False,
|
34 |
)
|
35 |
|
|
|
36 |
response = chat_completion.choices[0].message.content
|
37 |
|
|
|
38 |
if "Did you know!" in response and not response.endswith("\n"):
|
39 |
+
response = response.replace("Did you know!", "\nDid you know! π")
|
40 |
+
elif "Did you know!" in response:
|
41 |
+
response = response.replace("Did you know!", "Did you know! π")
|
42 |
|
43 |
return response
|
44 |
|
45 |
+
# Custom CSS for center alignment
|
46 |
+
custom_css = """
|
47 |
+
.center-text {
|
48 |
+
text-align: center;
|
49 |
+
margin: auto;
|
50 |
+
max-width: 90%;
|
51 |
+
}
|
52 |
+
"""
|
53 |
+
|
54 |
+
# Create the Gradio interface with custom CSS and updated title
|
55 |
+
with gr.Blocks(css=custom_css) as iface:
|
56 |
+
gr.Markdown(
|
57 |
+
"# πΎ Chat with the G.O.A.T. Roger Federer π",
|
58 |
+
elem_classes=["center-text"]
|
59 |
+
)
|
60 |
+
gr.Markdown(
|
61 |
+
"π Serve up your questions to the tennis legend! πΎ Get expert insights on Grand Slams, technique, and more. Let's rally some knowledge! π¬π",
|
62 |
+
elem_classes=["center-text"]
|
63 |
+
)
|
64 |
+
chatbot = gr.ChatInterface(
|
65 |
+
chat_with_federer,
|
66 |
+
examples=[
|
67 |
+
"What's your favorite Grand Slam tournament and why?",
|
68 |
+
"Can you explain the difference between clay and grass courts?",
|
69 |
+
"What do you think about the current state of men's tennis?",
|
70 |
+
"Any tips for improving my backhand?",
|
71 |
+
"What was your most memorable match and why?",
|
72 |
+
],
|
73 |
+
)
|
74 |
|
75 |
# Launch the interface
|
76 |
iface.launch()
|