umangshikarvar commited on
Commit
2a9a1e0
·
verified ·
1 Parent(s): b0595bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -7,11 +7,20 @@ import gradio as gr
7
  tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")
8
  tokenizer.pad_token = tokenizer.eos_token
9
 
10
- # === Load Model + QLoRA Adapter ===
11
- checkpoint_dir = "umangshikarvar/sentiment-gpt-neo-qlora" # Update if needed
12
  peft_config = PeftConfig.from_pretrained(checkpoint_dir)
13
- base_model = AutoModelForCausalLM.from_pretrained(peft_config.base_model_name_or_path, torch_dtype=torch.float16)
 
 
 
 
 
 
 
 
14
  model = PeftModel.from_pretrained(base_model, checkpoint_dir)
 
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
  model.eval().to(device)
17
 
@@ -58,10 +67,13 @@ def predict_sentiment(tweet):
58
  return "Neutral"
59
 
60
  # === Gradio Interface ===
61
- gr.Interface(
62
  fn=predict_sentiment,
63
  inputs=gr.Textbox(lines=2, placeholder="Enter the text", label="Statement"),
64
- outputs="text",
65
  title="Sentiment Classifier",
66
- description="Classifies the sentiment of a statement, as Positive, Negative, or Neutral."
67
- ).launch(share=True)
 
 
 
 
7
  tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")
8
  tokenizer.pad_token = tokenizer.eos_token
9
 
10
+ # === Load Model + QLoRA Adapter from Hugging Face Hub ===
11
+ checkpoint_dir = "umangshikarvar/sentiment-qlora-gptneo"
12
  peft_config = PeftConfig.from_pretrained(checkpoint_dir)
13
+
14
+ # Use float16 only if CUDA is available
15
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
16
+
17
+ base_model = AutoModelForCausalLM.from_pretrained(
18
+ peft_config.base_model_name_or_path,
19
+ torch_dtype=torch_dtype
20
+ )
21
+
22
  model = PeftModel.from_pretrained(base_model, checkpoint_dir)
23
+
24
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25
  model.eval().to(device)
26
 
 
67
  return "Neutral"
68
 
69
  # === Gradio Interface ===
70
+ demo = gr.Interface(
71
  fn=predict_sentiment,
72
  inputs=gr.Textbox(lines=2, placeholder="Enter the text", label="Statement"),
73
+ outputs=gr.Text(label="Predicted Sentiment"),
74
  title="Sentiment Classifier",
75
+ description="Classifies the sentiment of a statement as Positive, Negative, or Neutral."
76
+ )
77
+
78
+ # === Launch (set share=False for HF Spaces) ===
79
+ demo.launch(share=False)