Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,34 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
generator = pipeline('text-generation', model = 'isitcoding/gpt2_120_finetuned')
|
| 3 |
-
generator("", max_length = 1028, num_return_sequences=3)
|
| 4 |
-
|
| 5 |
-
'''import os
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
-
from huggingface_hub import InferenceClient
|
| 9 |
-
|
| 10 |
-
hf_token = os.getenv("gpt2_token")
|
| 11 |
-
# Initialize the text generation pipeline
|
| 12 |
-
client =
|
| 13 |
-
generator = pipeline("text-generation", )
|
| 14 |
-
|
| 15 |
-
# Define the response function with additional options for customization
|
| 16 |
-
def text_generation(
|
| 17 |
-
prompt: str,
|
| 18 |
-
details: bool = False,
|
| 19 |
-
stream: bool = False,
|
| 20 |
-
model: str = None,
|
| 21 |
-
best_of: int = None,
|
| 22 |
-
decoder_input_details: bool = None,
|
| 23 |
-
do_sample: bool = False,
|
| 24 |
-
frequency_penalty: float = None,
|
| 25 |
-
grammar: None = None,
|
| 26 |
-
max_new_tokens: int = None,
|
| 27 |
-
repetition_penalty: float = None
|
| 28 |
-
):
|
| 29 |
-
# Setup the configuration for the model generation
|
| 30 |
-
gen_params = {
|
| 31 |
-
"max_length": 518, # Default, you can tweak it or set from parameters
|
| 32 |
-
"num_return_sequences": 1,
|
| 33 |
-
"do_sample": do_sample,
|
| 34 |
-
"temperature": 0.7, # Controls randomness
|
| 35 |
-
"top_k": 50, # You can adjust for more control over sampling
|
| 36 |
-
"top_p": 0.9, # Same as above, for sampling
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
if max_new_tokens:
|
| 40 |
-
gen_params["max_length"] = max_new_tokens + len(prompt.split())
|
| 41 |
-
|
| 42 |
-
if frequency_penalty:
|
| 43 |
-
gen_params["frequency_penalty"] = frequency_penalty
|
| 44 |
-
|
| 45 |
-
if repetition_penalty:
|
| 46 |
-
gen_params["repetition_penalty"] = repetition_penalty
|
| 47 |
-
|
| 48 |
-
# Generate the text based on the input prompt and parameters
|
| 49 |
-
generated_text = generator(prompt, **gen_params)[0]["generated_text"]
|
| 50 |
-
|
| 51 |
-
if details:
|
| 52 |
-
# Return additional details for debugging if needed
|
| 53 |
-
return {
|
| 54 |
-
"generated_text": generated_text,
|
| 55 |
-
"params_used": gen_params
|
| 56 |
-
}
|
| 57 |
-
else:
|
| 58 |
-
return generated_text
|
| 59 |
-
|
| 60 |
-
# Create Gradio interface
|
| 61 |
-
iface = gr.Interface(
|
| 62 |
-
fn=text_generation, # The function we defined
|
| 63 |
-
inputs=[
|
| 64 |
-
gr.Textbox(label="Input Prompt"), # User input prompt
|
| 65 |
-
gr.Checkbox(label="Show Details", default=False), # Option for additional details
|
| 66 |
-
gr.Checkbox(label="Stream Mode", default=False), # Streaming checkbox (not used in this example)
|
| 67 |
-
gr.Textbox(label="Model (optional)", default=None), # Optional model name
|
| 68 |
-
gr.Slider(minimum=1, maximum=5, label="Best of (Optional)", default=None),
|
| 69 |
-
gr.Slider(minimum=0.0, maximum=2.0, label="Frequency Penalty (Optional)", default=None),
|
| 70 |
-
gr.Slider(minimum=0.0, maximum=2.0, label="Repetition Penalty (Optional)", default=None),
|
| 71 |
-
],
|
| 72 |
-
outputs="text" # Output is plain text
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
# Launch the interface
|
| 76 |
-
iface.launch()
|
| 77 |
-
'''
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
+
# Load the text generation pipeline with your fine-tuned model
|
| 8 |
+
generator = pipeline('text-generation', model='isitcoding/gpt2_120_finetuned')
|
| 9 |
+
|
| 10 |
+
# Function to generate responses using the text generation model
|
| 11 |
+
def respond(message, chat_history):
|
| 12 |
+
# Generate a response from the model
|
| 13 |
+
response = generator(message, max_length=1028, num_return_sequences=3)[0]['generated_text']
|
| 14 |
+
# Append the user message and model response to chat history
|
| 15 |
+
chat_history.append(("User", message))
|
| 16 |
+
chat_history.append(("Bot", response))
|
| 17 |
+
return chat_history
|
| 18 |
+
|
| 19 |
+
# Create a Gradio interface using Blocks
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
# Add a Chatbot component
|
| 22 |
+
chatbot = gr.Chatbot()
|
| 23 |
+
# Add a textbox for user input
|
| 24 |
+
msg = gr.Textbox(label="Enter your message")
|
| 25 |
+
# Add a button to clear the chat
|
| 26 |
+
clear = gr.Button("Clear")
|
| 27 |
+
|
| 28 |
+
# Define what happens when the user submits a message
|
| 29 |
+
msg.submit(respond, [msg, chatbot], chatbot)
|
| 30 |
+
# Define what happens when the clear button is pressed
|
| 31 |
+
clear.click(lambda: [], None, chatbot)
|
| 32 |
+
|
| 33 |
+
# Launch the Gradio interface
|
| 34 |
+
demo.launch()
|