JimminDev commited on
Commit
5379e57
·
verified ·
1 Parent(s): a32dacf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -9,46 +9,65 @@ except ImportError:
9
  from transformers import pipeline
10
  import gradio as gr
11
 
12
- # Initialize sentiment_analysis as None
13
- sentiment_analysis = None
 
14
 
15
- # Attempt to load the model and run a test prediction
16
  try:
17
- # Explicitly specify the model
18
- model_name = "distilbert-base-uncased-finetuned-sst-2-english"
19
- print("Loading model...")
20
- sentiment_analysis = pipeline("sentiment-analysis", model=model_name)
21
- test_output = sentiment_analysis("Testing the model with a simple sentence.")
22
- print("Model test output:", test_output)
 
 
 
 
 
23
  except Exception as e:
24
- print(f"Failed to load or run model: {e}")
25
 
26
- # Prediction function with error handling
27
- def predict_sentiment(text):
28
  try:
29
- if sentiment_analysis is None:
30
- raise ValueError("Model not loaded.")
31
- predictions = sentiment_analysis(text)
 
 
 
 
 
 
 
 
32
  return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
33
  except Exception as e:
34
  return f"Error processing input: {e}"
35
 
36
  # Define example inputs
37
  examples = [
38
- "I absolutely love this product! It has changed my life.",
39
- "This is the worst movie I have ever seen. Completely disappointing.",
40
- "I'm not sure how I feel about this new update. It has some good points, but also many drawbacks.",
41
- "The customer service was fantastic! Very helpful and polite.",
42
- "Honestly, this was quite a mediocre experience. Nothing special."
43
  ]
44
 
45
  # Gradio interface setup
46
- iface = gr.Interface(fn=predict_sentiment,
47
- title="Sentiment Analysis",
48
- description="Enter text to analyze sentiment. Powered by Hugging Face Transformers.",
49
- inputs="text",
50
- outputs="text",
51
- examples=examples)
 
 
 
 
 
52
 
53
  if __name__ == "__main__":
54
- iface.launch()
 
9
  from transformers import pipeline
10
  import gradio as gr
11
 
12
+ # Initialize models as None
13
+ model1 = None
14
+ model2 = None
15
 
16
+ # Attempt to load the models and run test predictions
17
  try:
18
+ model1_name = "JimminDev/jim-text-class"
19
+ model2_name = "JimminDev/Depressive-detector"
20
+ print("Loading models...")
21
+
22
+ model1 = pipeline("text-classification", model=model1_name)
23
+ test_output1 = model1("Testing the first model with a simple sentence.")
24
+ print("Model 1 test output:", test_output1)
25
+
26
+ model2 = pipeline("text-classification", model=model2_name)
27
+ test_output2 = model2("Testing the second model with a simple sentence.")
28
+ print("Model 2 test output:", test_output2)
29
  except Exception as e:
30
+ print(f"Failed to load or run models: {e}")
31
 
32
+ # Prediction function with model selection and error handling
33
+ def predict_sentiment(text, model_choice):
34
  try:
35
+ if model_choice == "Model 1":
36
+ if model1 is None:
37
+ raise ValueError("Model 1 not loaded.")
38
+ predictions = model1(text)
39
+ elif model_choice == "Model 2":
40
+ if model2 is None:
41
+ raise ValueError("Model 2 not loaded.")
42
+ predictions = model2(text)
43
+ else:
44
+ raise ValueError("Invalid model choice.")
45
+
46
  return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
47
  except Exception as e:
48
  return f"Error processing input: {e}"
49
 
50
  # Define example inputs
51
  examples = [
52
+ ["I absolutely love this product! It has changed my life.", "Model 1"],
53
+ ["This is the worst movie I have ever seen. Completely disappointing.", "Model 1"],
54
+ ["I'm not sure how I feel about this new update. It has some good points, but also many drawbacks.", "Model 2"],
55
+ ["The customer service was fantastic! Very helpful and polite.", "Model 2"],
56
+ ["Honestly, this was quite a mediocre experience. Nothing special.", "Model 1"]
57
  ]
58
 
59
  # Gradio interface setup
60
+ iface = gr.Interface(
61
+ fn=predict_sentiment,
62
+ title="Sentiment Analysis",
63
+ description="Enter text to analyze sentiment. Powered by Hugging Face Transformers.",
64
+ inputs=[
65
+ gr.inputs.Textbox(lines=2, placeholder="Enter text here..."),
66
+ gr.inputs.Radio(choices=["Model 1", "Model 2"], label="Select Model")
67
+ ],
68
+ outputs="text",
69
+ examples=examples
70
+ )
71
 
72
  if __name__ == "__main__":
73
+ iface.launch()