File size: 1,637 Bytes
ab86870 3680a42 ab86870 3680a42 2a4edc1 3680a42 2a4edc1 ab86870 fc4cc2c 7fb7b85 13cba81 f7750ee 13cba81 f7750ee fc4cc2c 13cba81 892e1e5 1694eaa 892e1e5 fc4cc2c ab86870 7fb7b85 0eed6d3 8937d91 0eed6d3 ab86870 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import gradio as gr
from transformers import pipeline
import torch
# Check if a GPU is available
device = 0 if torch.cuda.is_available() else -1
# Load the text-generation pipeline with the appropriate device
model = pipeline(
"text-generation",
model="rish13/polymers",
device=device # Automatically use GPU if available, otherwise CPU
)
def remove_duplicate_sentences(text):
# Split text into sentences (assuming sentences are separated by periods)
sentences = text.split('. ')
unique_sentences = list(dict.fromkeys(sentences)) # Remove exact duplicates
return '. '.join(unique_sentences)
def generate_response(prompt):
# Generate text from the model
response = model(
prompt,
max_length=130, # Adjusted to generate shorter text
num_return_sequences=1,
temperature=0.7, # Increased to add more randomness
top_k=130, # Increased to allow a wider selection of words
top_p=0.95 # Slightly increased cumulative probability threshold
)
# Get the generated text from the response
generated_text = response[0]['generated_text']
# Post-process to remove duplicate sentences
processed_text = remove_duplicate_sentences(generated_text)
return processed_text
# Define the Gradio interface
interface = gr.Interface(
fn=generate_response,
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
outputs="text",
title="Polymer Knowledge Model",
description="A model fine-tuned for generating text related to polymers."
)
# Launch the interface
interface.launch()
|