Besimplestudio commited on
Commit
ee04223
·
verified ·
1 Parent(s): ceff078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -54
app.py CHANGED
@@ -1,63 +1,58 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
-
4
- # Load Hugging Face models
5
- # Text generation for keyword suggestions
6
- keyword_generator = pipeline("text-generation", model="gpt2", tokenizer="gpt2")
7
-
8
- # Sentiment analysis for niche review insights
9
- sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
10
-
11
-
12
- # Function to generate keyword suggestions
 
 
 
 
13
  def suggest_keywords(prompt):
14
- results = keyword_generator(prompt, max_length=50, num_return_sequences=3)
15
- suggestions = [res["text"].strip() for res in results]
16
- return "\n".join(suggestions)
17
-
18
-
19
- # Function to analyze sentiment of user-input text
 
 
 
 
 
20
  def analyze_sentiment(text):
21
- sentiments = sentiment_analyzer(text)
22
- return sentiments
 
 
 
 
 
 
 
 
 
23
 
24
-
25
- # Gradio Interface Design
26
- with gr.Blocks() as app:
27
- gr.Markdown(
28
- """
29
- # KDP Keyword Suggestion App
30
- Generate profitable KDP coloring book niches and analyze customer feedback!
31
- """
32
- )
33
-
34
- # Section for keyword generation
35
  with gr.Row():
36
  with gr.Column():
37
- prompt_input = gr.Textbox(
38
- label="Enter Keyword Prompt",
39
- placeholder="E.g., coloring book for kids about",
40
- )
41
- keyword_output = gr.Textbox(label="Generated Keywords", lines=5)
42
-
43
- keyword_button = gr.Button("Generate Keywords")
44
- keyword_button.click(suggest_keywords, inputs=prompt_input, outputs=keyword_output)
45
-
46
- # Section for sentiment analysis
47
- with gr.Row():
48
  with gr.Column():
49
- review_input = gr.Textbox(
50
- label="Enter Text for Sentiment Analysis",
51
- placeholder="Paste a customer review or feedback here...",
52
- lines=4,
53
- )
54
- sentiment_output = gr.Label(label="Sentiment Analysis Result")
55
-
56
- sentiment_button = gr.Button("Analyze Sentiment")
57
- sentiment_button.click(analyze_sentiment, inputs=review_input, outputs=sentiment_output)
58
 
59
- # Footer
60
- gr.Markdown("Built with ❤️ using Hugging Face and Gradio for KDP enthusiasts!")
61
 
62
- # Launch the app
63
- app.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # Initialize the keyword generator pipeline with error handling
5
+ try:
6
+ # Load model and tokenizer
7
+ model_name = "gpt2"
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ keyword_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
+ print("Model loaded successfully!")
12
+ except Exception as e:
13
+ keyword_generator = None
14
+ print(f"Error loading model: {e}")
15
+
16
+ # Function to generate keywords
17
  def suggest_keywords(prompt):
18
+ if not keyword_generator:
19
+ return "Model failed to load. Please check the logs or environment."
20
+
21
+ try:
22
+ results = keyword_generator(prompt, max_length=50, num_return_sequences=3)
23
+ suggestions = [res["text"].strip() for res in results]
24
+ return "\n".join(suggestions)
25
+ except Exception as e:
26
+ return f"Error generating keywords: {e}"
27
+
28
+ # Function for sentiment analysis
29
  def analyze_sentiment(text):
30
+ try:
31
+ sentiment_pipeline = pipeline("sentiment-analysis")
32
+ result = sentiment_pipeline(text)[0]
33
+ return f"Label: {result['label']}, Confidence: {result['score']:.2f}"
34
+ except Exception as e:
35
+ return f"Error performing sentiment analysis: {e}"
36
+
37
+ # Gradio Interface
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("# KDP Keyword Suggestion App")
40
+ gr.Markdown("Generate profitable KDP coloring book niches and analyze customer feedback!")
41
 
 
 
 
 
 
 
 
 
 
 
 
42
  with gr.Row():
43
  with gr.Column():
44
+ gr.Markdown("### Enter Keyword Prompt")
45
+ keyword_input = gr.Textbox(label="Enter Keyword Prompt")
46
+ keyword_output = gr.Textbox(label="Generated Keywords")
47
+ generate_button = gr.Button("Generate Keywords")
48
+
 
 
 
 
 
 
49
  with gr.Column():
50
+ gr.Markdown("### Enter Text for Sentiment Analysis")
51
+ sentiment_input = gr.Textbox(label="Paste a customer review or feedback here")
52
+ sentiment_output = gr.Textbox(label="Sentiment Analysis Result")
53
+ sentiment_button = gr.Button("Analyze Sentiment")
 
 
 
 
 
54
 
55
+ generate_button.click(suggest_keywords, inputs=keyword_input, outputs=keyword_output)
56
+ sentiment_button.click(analyze_sentiment, inputs=sentiment_input, outputs=sentiment_output)
57
 
58
+ demo.launch()