Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
8 |
-
"""
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
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 |
-
|
29 |
-
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
# Create
|
37 |
iface = gr.Interface(
|
38 |
-
fn=
|
39 |
-
inputs=[
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
)
|
43 |
|
44 |
-
# Launch the
|
45 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Initialize the text generation pipeline
|
5 |
+
generator = pipeline("text-generation", model="gpt2", tokenizer="gpt2")
|
|
|
|
|
6 |
|
7 |
+
# Define the response function with additional options for customization
|
8 |
+
def text_generation(
|
9 |
+
prompt: str,
|
10 |
+
details: bool = False,
|
11 |
+
stream: bool = False,
|
12 |
+
model: str = None,
|
13 |
+
best_of: int = None,
|
14 |
+
decoder_input_details: bool = None,
|
15 |
+
do_sample: bool = False,
|
16 |
+
frequency_penalty: float = None,
|
17 |
+
grammar: None = None,
|
18 |
+
max_new_tokens: int = None,
|
19 |
+
repetition_penalty: float = None
|
20 |
+
):
|
21 |
+
# Setup the configuration for the model generation
|
22 |
+
gen_params = {
|
23 |
+
"max_length": 50, # Default, you can tweak it or set from parameters
|
24 |
+
"num_return_sequences": 1,
|
25 |
+
"do_sample": do_sample,
|
26 |
+
"temperature": 0.7, # Controls randomness
|
27 |
+
"top_k": 50, # You can adjust for more control over sampling
|
28 |
+
"top_p": 0.9, # Same as above, for sampling
|
29 |
+
}
|
30 |
|
31 |
+
if max_new_tokens:
|
32 |
+
gen_params["max_length"] = max_new_tokens + len(prompt.split())
|
33 |
|
34 |
+
if frequency_penalty:
|
35 |
+
gen_params["frequency_penalty"] = frequency_penalty
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
if repetition_penalty:
|
38 |
+
gen_params["repetition_penalty"] = repetition_penalty
|
39 |
|
40 |
+
# Generate the text based on the input prompt and parameters
|
41 |
+
generated_text = generator(prompt, **gen_params)[0]["generated_text"]
|
42 |
|
43 |
+
if details:
|
44 |
+
# Return additional details for debugging if needed
|
45 |
+
return {
|
46 |
+
"generated_text": generated_text,
|
47 |
+
"params_used": gen_params
|
48 |
+
}
|
49 |
+
else:
|
50 |
+
return generated_text
|
51 |
|
52 |
+
# Create Gradio interface
|
53 |
iface = gr.Interface(
|
54 |
+
fn=text_generation, # The function we defined
|
55 |
+
inputs=[
|
56 |
+
gr.Textbox(label="Input Prompt"), # User input prompt
|
57 |
+
gr.Checkbox(label="Show Details", default=False), # Option for additional details
|
58 |
+
gr.Checkbox(label="Stream Mode", default=False), # Streaming checkbox (not used in this example)
|
59 |
+
gr.Textbox(label="Model (optional)", default=None), # Optional model name
|
60 |
+
gr.Slider(minimum=1, maximum=5, label="Best of (Optional)", default=None),
|
61 |
+
gr.Slider(minimum=0.0, maximum=2.0, label="Frequency Penalty (Optional)", default=None),
|
62 |
+
gr.Slider(minimum=0.0, maximum=2.0, label="Repetition Penalty (Optional)", default=None),
|
63 |
+
],
|
64 |
+
outputs="text" # Output is plain text
|
65 |
)
|
66 |
|
67 |
+
# Launch the interface
|
68 |
iface.launch()
|