Update app.py
Browse files
app.py
CHANGED
|
@@ -14,33 +14,43 @@ model = pipeline(
|
|
| 14 |
|
| 15 |
def generate_response(prompt):
|
| 16 |
# Generate text from the model
|
| 17 |
-
response = model(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Get the generated text from the response
|
| 20 |
generated_text = response[0]['generated_text']
|
| 21 |
-
|
| 22 |
-
# Find the position of the first end-of-sentence punctuation
|
| 23 |
-
end_punctuation = ['.', '!', '?']
|
| 24 |
-
end_position = -1
|
| 25 |
-
for punct in end_punctuation:
|
| 26 |
-
pos = generated_text.find(punct)
|
| 27 |
-
if pos != -1 and (end_position == -1 or pos < end_position):
|
| 28 |
-
end_position = pos
|
| 29 |
-
|
| 30 |
-
# If punctuation is found, truncate the text at that point
|
| 31 |
-
if end_position != -1:
|
| 32 |
-
generated_text = generated_text[:end_position + 1]
|
| 33 |
|
| 34 |
return generated_text
|
| 35 |
|
| 36 |
# Define the Gradio interface
|
| 37 |
interface = gr.Interface(
|
| 38 |
fn=generate_response,
|
| 39 |
-
inputs=gr.Textbox(
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
title="Polymer Knowledge Model",
|
| 42 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
# Launch the interface
|
| 46 |
interface.launch()
|
|
|
|
|
|
| 14 |
|
| 15 |
def generate_response(prompt):
|
| 16 |
# Generate text from the model
|
| 17 |
+
response = model(
|
| 18 |
+
prompt,
|
| 19 |
+
max_length=50, # Adjusted to generate shorter text
|
| 20 |
+
num_return_sequences=1,
|
| 21 |
+
temperature=0.5, # Lowered to reduce randomness
|
| 22 |
+
top_k=50, # Limiting the next word selection
|
| 23 |
+
top_p=0.9 # Cumulative probability threshold
|
| 24 |
+
)
|
| 25 |
|
| 26 |
# Get the generated text from the response
|
| 27 |
generated_text = response[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
return generated_text
|
| 30 |
|
| 31 |
# Define the Gradio interface
|
| 32 |
interface = gr.Interface(
|
| 33 |
fn=generate_response,
|
| 34 |
+
inputs=gr.Textbox(
|
| 35 |
+
lines=2,
|
| 36 |
+
placeholder="Enter your prompt here...",
|
| 37 |
+
label="Prompt",
|
| 38 |
+
elem_id="input-textbox" # Custom styling ID for input textbox
|
| 39 |
+
),
|
| 40 |
+
outputs=gr.Textbox(
|
| 41 |
+
label="Generated Text",
|
| 42 |
+
elem_id="output-textbox" # Custom styling ID for output textbox
|
| 43 |
+
),
|
| 44 |
title="Polymer Knowledge Model",
|
| 45 |
+
description=(
|
| 46 |
+
"This application uses a fine-tuned model to generate text related to polymers. "
|
| 47 |
+
"Enter a prompt to get started, and the model will generate relevant text."
|
| 48 |
+
),
|
| 49 |
+
theme="huggingface", # Apply a theme for consistent styling
|
| 50 |
+
layout="horizontal", # Arrange input and output side by side
|
| 51 |
+
live=True # Update the output live as the user types
|
| 52 |
)
|
| 53 |
|
| 54 |
# Launch the interface
|
| 55 |
interface.launch()
|
| 56 |
+
|