Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,45 @@
|
|
| 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:
|
|
|
|
| 6 |
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
response =
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", 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 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
"""
|
| 6 |
+
For more information on `huggingface_hub` Inference API support, please check the docs:
|
| 7 |
+
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 8 |
"""
|
|
|
|
| 9 |
|
| 10 |
+
# Initialize the inference client with the model you're using
|
| 11 |
+
client = InferenceClient(model="isitcoding/gpt2_120_finetuned")
|
| 12 |
+
|
| 13 |
+
# Initialize a text generation pipeline using Hugging Face's transformer
|
| 14 |
+
generator = pipeline('text-generation', model='HuggingFaceH4/zephyr-7b-beta')
|
| 15 |
+
|
| 16 |
+
def respond(message, history: list[tuple[str, str]]):
|
| 17 |
+
"""
|
| 18 |
+
Respond function to generate text based on the user's message and conversation history.
|
| 19 |
+
The `history` parameter keeps track of the conversation context.
|
| 20 |
+
"""
|
| 21 |
+
# Add the new message to the conversation history
|
| 22 |
+
history.append(("User", message))
|
| 23 |
+
|
| 24 |
+
# Use the generator model to get a response from the model
|
| 25 |
+
input_text = " ".join([h[1] for h in history]) # Combine the conversation history into one string
|
| 26 |
+
output = generator(input_text, max_length=500, num_return_sequences=1)
|
| 27 |
+
|
| 28 |
+
# Extract the response from the output
|
| 29 |
+
response = output[0]['generated_text'].strip()
|
| 30 |
+
|
| 31 |
+
# Add the model's response to the history
|
| 32 |
+
history.append(("Bot", response))
|
| 33 |
+
|
| 34 |
+
return response, history
|
| 35 |
+
|
| 36 |
+
# Create a Gradio interface for interaction
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=respond,
|
| 39 |
+
inputs=[gr.Textbox(label="Enter your message", placeholder="Type here..."), gr.State()],
|
| 40 |
+
outputs=[gr.Textbox(label="Response"), gr.State()],
|
| 41 |
+
live=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
+
# Launch the Gradio interface
|
| 45 |
+
iface.launch()
|
|
|