File size: 1,280 Bytes
f6cb791
7dc916b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6cb791
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM

# Load the PEFT model configuration and model
config = PeftConfig.from_pretrained("isashap/contexttrained-validationloss-gpt2FINAL")
base_model = AutoModelForCausalLM.from_pretrained("gpt2")
model = PeftModel.from_pretrained(base_model, "isashap/contexttrained-validationloss-gpt2FINAL")

# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("gpt2")

# Define the prediction function
def predict_stock(input_text):
    # Tokenize the input
    inputs = tokenizer(input_text, return_tensors="pt")
    # Generate output using the model
    outputs = model.generate(**inputs, max_length=50, num_return_sequences=1)
    # Decode the output
    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()