Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,25 +9,31 @@ with open("BACKGROUND.md", "r", encoding="utf-8") as f:
|
|
9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
|
11 |
def respond(
|
12 |
-
message,
|
13 |
-
history: list[
|
14 |
-
system_message,
|
15 |
-
max_tokens,
|
16 |
-
temperature,
|
17 |
-
top_p,
|
18 |
):
|
|
|
|
|
|
|
|
|
19 |
messages = [{"role": "system", "content": system_message}]
|
20 |
-
|
21 |
-
for val in history:
|
22 |
-
if val[0]:
|
23 |
-
messages.append({"role": "user", "content": val[0]})
|
24 |
-
if val[1]:
|
25 |
-
messages.append({"role": "assistant", "content": val[1]})
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
messages.append({"role": "user", "content": message})
|
28 |
|
|
|
29 |
response = ""
|
30 |
-
|
31 |
for msg in client.chat_completion(
|
32 |
messages,
|
33 |
max_tokens=max_tokens,
|
@@ -37,26 +43,26 @@ def respond(
|
|
37 |
):
|
38 |
token = msg.choices[0].delta.content
|
39 |
response += token
|
40 |
-
# 'yield' returns partial responses for streaming
|
41 |
yield response
|
42 |
|
43 |
-
# Step 3: Build a Gradio Blocks interface with
|
44 |
with gr.Blocks() as demo:
|
45 |
-
#
|
46 |
with gr.Tab("GPT Chat Agent"):
|
47 |
gr.Markdown("## Welcome to Varun's GPT Agent")
|
48 |
gr.Markdown("Feel free to ask questions about Varun’s journey, skills, and more!")
|
49 |
chat = gr.ChatInterface(
|
50 |
-
respond,
|
51 |
additional_inputs=[
|
52 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
53 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
54 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
55 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
56 |
],
|
|
|
57 |
)
|
58 |
|
59 |
-
#
|
60 |
with gr.Tab("Varun's Background"):
|
61 |
gr.Markdown("# About Varun")
|
62 |
gr.Markdown(background_text)
|
|
|
9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
|
11 |
def respond(
|
12 |
+
message: str,
|
13 |
+
history: list[dict],
|
14 |
+
system_message: str,
|
15 |
+
max_tokens: int,
|
16 |
+
temperature: float,
|
17 |
+
top_p: float,
|
18 |
):
|
19 |
+
if history is None:
|
20 |
+
history = []
|
21 |
+
|
22 |
+
# Include system message
|
23 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# Append history in the required format
|
26 |
+
for interaction in history:
|
27 |
+
if "user" in interaction:
|
28 |
+
messages.append({"role": "user", "content": interaction["user"]})
|
29 |
+
if "assistant" in interaction:
|
30 |
+
messages.append({"role": "assistant", "content": interaction["assistant"]})
|
31 |
+
|
32 |
+
# Append the current user message
|
33 |
messages.append({"role": "user", "content": message})
|
34 |
|
35 |
+
# Generate response from the model
|
36 |
response = ""
|
|
|
37 |
for msg in client.chat_completion(
|
38 |
messages,
|
39 |
max_tokens=max_tokens,
|
|
|
43 |
):
|
44 |
token = msg.choices[0].delta.content
|
45 |
response += token
|
|
|
46 |
yield response
|
47 |
|
48 |
+
# Step 3: Build a Gradio Blocks interface with Tabs
|
49 |
with gr.Blocks() as demo:
|
50 |
+
# Tab 1: GPT Chat Agent
|
51 |
with gr.Tab("GPT Chat Agent"):
|
52 |
gr.Markdown("## Welcome to Varun's GPT Agent")
|
53 |
gr.Markdown("Feel free to ask questions about Varun’s journey, skills, and more!")
|
54 |
chat = gr.ChatInterface(
|
55 |
+
fn=respond,
|
56 |
additional_inputs=[
|
57 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
58 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
59 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
60 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
61 |
],
|
62 |
+
type="messages", # Use 'messages' type for Gradio's ChatInterface
|
63 |
)
|
64 |
|
65 |
+
# Tab 2: Background Document
|
66 |
with gr.Tab("Varun's Background"):
|
67 |
gr.Markdown("# About Varun")
|
68 |
gr.Markdown(background_text)
|