|
import gradio as gr |
|
from transformers import AutoTokenizer |
|
from peft import PeftModel, PeftConfig |
|
from transformers import AutoModelForCausalLM |
|
|
|
|
|
config = PeftConfig.from_pretrained("isashap/contexttrained-validationloss-gpt2FINAL") |
|
base_model = AutoModelForCausalLM.from_pretrained("gpt2") |
|
model = PeftModel.from_pretrained(base_model, "isashap/contexttrained-validationloss-gpt2FINAL") |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("gpt2") |
|
|
|
|
|
def predict_stock(input_text): |
|
|
|
inputs = tokenizer(input_text, return_tensors="pt") |
|
|
|
outputs = model.generate(**inputs, max_length=50, num_return_sequences=1) |
|
|
|
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
return generated_text |
|
|
|
demo = gr.Interface( |
|
fn=predict_stock, |
|
inputs=gr.Textbox(lines=2, label="Input Text (e.g., stock-related news)"), |
|
outputs=gr.Textbox(label="Prediction"), |
|
title="Stock Prediction with PEFT Model", |
|
description="A demo that uses a fine-tuned GPT-2 model with PEFT to predict stock outcomes based on input text." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|