Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Initialize the Groq client
|
| 6 |
+
client = Groq(
|
| 7 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
)
|
| 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 a short, funny "Did you know!" anecdote related to tennis. The anecdote should be no more than 15 words.
|
| 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 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": full_prompt,
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
model="mixtral-8x7b-32768",
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
max_tokens=1000,
|
| 37 |
+
top_p=1,
|
| 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 |
+
# Create the Gradio interface
|
| 51 |
+
iface = gr.ChatInterface(
|
| 52 |
+
chat_with_federer,
|
| 53 |
+
title="πΎ Chat with the G.O.A.T. Roger Federer π",
|
| 54 |
+
description="π Serve up your questions to the tennis legend! πΎ Get expert insights on Grand Slams, technique, and more. Let's rally some knowledge! π¬π",
|
| 55 |
+
theme="soft",
|
| 56 |
+
examples=[
|
| 57 |
+
"What's your favorite Grand Slam tournament and why?",
|
| 58 |
+
"Can you explain the difference between clay and grass courts?",
|
| 59 |
+
"What do you think about the current state of men's tennis?",
|
| 60 |
+
"Any tips for improving my backhand?",
|
| 61 |
+
"What was your most memorable match and why?",
|
| 62 |
+
],
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
# Launch the interface
|
| 66 |
+
iface.launch()
|