Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
def suggest_keywords(prompt):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
def analyze_sentiment(text):
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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 |
-
|
60 |
-
|
61 |
|
62 |
-
|
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()
|
|