Facececersek commited on
Commit
dab1a3c
·
verified ·
1 Parent(s): cc05395

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Initialize the sentiment analysis pipeline
5
+ sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
6
+
7
+ # Initialize text generation pipeline
8
+ generator = pipeline("text-generation", model="distilgpt2", max_length=100)
9
+
10
+ def analyze_sentiment(text):
11
+ """Analyze the sentiment of the input text."""
12
+ if not text.strip():
13
+ return "Please enter some text to analyze."
14
+
15
+ result = sentiment_pipeline(text)[0]
16
+ label = result['label']
17
+ score = result['score']
18
+
19
+ return f"Sentiment: {label}\nConfidence: {score:.2%}"
20
+
21
+ def generate_text(prompt):
22
+ """Generate text based on the input prompt."""
23
+ if not prompt.strip():
24
+ return "Please enter a prompt to generate text."
25
+
26
+ result = generator(prompt, max_length=100, num_return_sequences=1)
27
+ return result[0]['generated_text']
28
+
29
+ def chatbot(message, history):
30
+ """Simple chatbot function."""
31
+ if not message.strip():
32
+ return "Please say something!"
33
+
34
+ # Simple response logic
35
+ message_lower = message.lower()
36
+
37
+ if "hello" in message_lower or "hi" in message_lower:
38
+ return "Hello! How can I help you today?"
39
+ elif "how are you" in message_lower:
40
+ return "I'm doing great, thanks for asking! I'm an AI assistant ready to help."
41
+ elif "bye" in message_lower:
42
+ return "Goodbye! Have a great day!"
43
+ elif "sentiment" in message_lower:
44
+ return "I can analyze sentiment! Go to the Sentiment Analysis tab and enter some text."
45
+ else:
46
+ # Use the generator for other inputs
47
+ response = generator(message, max_length=50, num_return_sequences=1)
48
+ return response[0]['generated_text']
49
+
50
+ # Create the Gradio interface with tabs
51
+ with gr.Blocks(title="Basic AI Assistant") as demo:
52
+ gr.Markdown("# 🤖 Basic AI Assistant")
53
+ gr.Markdown("A simple AI-powered assistant with multiple capabilities!")
54
+
55
+ with gr.Tabs():
56
+ with gr.TabItem("💬 Chat"):
57
+ gr.Markdown("Chat with the AI assistant!")
58
+ chat_interface = gr.ChatInterface(chatbot, type="messages")
59
+
60
+ with gr.TabItem("😊 Sentiment Analysis"):
61
+ gr.Markdown("Analyze the sentiment of your text (positive or negative).")
62
+ with gr.Row():
63
+ sentiment_input = gr.Textbox(
64
+ label="Enter text to analyze",
65
+ placeholder="Type something like 'I love this product!'",
66
+ lines=3
67
+ )
68
+ sentiment_output = gr.Textbox(label="Result", lines=2)
69
+ sentiment_btn = gr.Button("Analyze Sentiment", variant="primary")
70
+ sentiment_btn.click(analyze_sentiment, inputs=sentiment_input, outputs=sentiment_output)
71
+
72
+ with gr.TabItem("✍️ Text Generation"):
73
+ gr.Markdown("Generate text based on a prompt!")
74
+ with gr.Row():
75
+ gen_input = gr.Textbox(
76
+ label="Enter your prompt",
77
+ placeholder="Once upon a time...",
78
+ lines=3
79
+ )
80
+ gen_output = gr.Textbox(label="Generated Text", lines=5)
81
+ gen_btn = gr.Button("Generate Text", variant="primary")
82
+ gen_btn.click(generate_text, inputs=gen_input, outputs=gen_output)
83
+
84
+ gr.Markdown("---")
85
+ gr.Markdown("Made with ❤️ using Gradio and Hugging Face Transformers")
86
+
87
+ if __name__ == "__main__":
88
+ demo.launch()