Spaces:
Sleeping
Sleeping
good
Browse files
app.py
CHANGED
@@ -1,32 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def respond(
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
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,
|
@@ -35,30 +38,31 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
-
yield response
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot named Tirth.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import wikipediaapi
|
4 |
|
5 |
+
# Initialize inference client for chat
|
|
|
|
|
6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
7 |
+
# Initialize Wikipedia API
|
8 |
+
wiki_wiki = wikipediaapi.Wikipedia('en')
|
9 |
|
10 |
+
def search_wikipedia(query):
|
11 |
+
page = wiki_wiki.page(query)
|
12 |
+
if page.exists():
|
13 |
+
return page.summary
|
14 |
+
else:
|
15 |
+
return "No information found on that topic."
|
16 |
|
17 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
18 |
+
# Search Wikipedia for information
|
19 |
+
search_response = search_wikipedia(message)
|
20 |
+
|
21 |
+
# Prepare the chat messages
|
|
|
|
|
|
|
22 |
messages = [{"role": "system", "content": system_message}]
|
|
|
23 |
for val in history:
|
24 |
if val[0]:
|
25 |
messages.append({"role": "user", "content": val[0]})
|
26 |
if val[1]:
|
27 |
messages.append({"role": "assistant", "content": val[1]})
|
28 |
+
|
29 |
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
# Generate response from chat model
|
32 |
response = ""
|
|
|
33 |
for message in client.chat_completion(
|
34 |
messages,
|
35 |
max_tokens=max_tokens,
|
|
|
38 |
top_p=top_p,
|
39 |
):
|
40 |
token = message.choices[0].delta.content
|
|
|
41 |
response += token
|
42 |
+
yield response, search_response # Return both responses
|
43 |
|
44 |
+
# Gradio interface setup using Blocks
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("## Chatbot with Wikipedia Search")
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
system_message = gr.Textbox(value="You are a friendly Chatbot named Tirth.", label="System message")
|
50 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
51 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
52 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
53 |
+
|
54 |
+
with gr.Column():
|
55 |
+
chat_output = gr.Chatbox(label="Chat History")
|
56 |
+
user_input = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
57 |
+
submit_btn = gr.Button("Send")
|
58 |
|
59 |
+
# Function to handle button click
|
60 |
+
def on_submit(message, history):
|
61 |
+
response, search_response = respond(message, history, system_message.value, max_tokens.value, temperature.value, top_p.value)
|
62 |
+
return history + [(message, response)], search_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# Connect the button to the submission function
|
65 |
+
submit_btn.click(on_submit, inputs=[user_input, chat_output], outputs=[chat_output, gr.Textbox(label="Wikipedia Summary")])
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
demo.launch()
|