Agentic-NLP / app.py
GermanySutherland's picture
Create app.py
dddfb60 verified
import gradio as gr
from transformers import pipeline
# Load a free Hugging Face model (small + free to run)
generator = pipeline("text2text-generation", model="google/flan-t5-small")
# Agent function
def agentic_ai(user_input):
# Step 1: Analyze input
analysis_prompt = f"Analyze the intent of this input: {user_input}"
analysis = generator(analysis_prompt, max_length=50, do_sample=False)[0]['generated_text']
# Step 2: Decide what to do (simple rule-based agent)
if "summarize" in user_input.lower():
task_prompt = f"Summarize this text in 2 lines: {user_input}"
elif "question" in user_input.lower() or "?" in user_input:
task_prompt = f"Answer this question briefly: {user_input}"
else:
task_prompt = f"Generate a helpful response: {user_input}"
# Step 3: LLM Response
response = generator(task_prompt, max_length=80, do_sample=False)[0]['generated_text']
# Step 4: Return both analysis + final response
return f"🔎 Agent Analysis: {analysis}\n\n💡 Agent Response: {response}"
# Tutorial text (20 bullets)
tutorial_text = """
---
## 📘 How to Use this Web App (Step by Step)
1. Open the app on Hugging Face.
2. You’ll see a text box in the center.
3. Type your question, statement, or text.
4. Click the orange **Submit** button.
5. The AI will analyze your input first.
6. You will see **Agent Analysis** (what AI thinks you mean).
7. You will see **Agent Response** (the final helpful reply).
8. If you ask a question (with “?”), AI gives an answer.
9. If you ask to **summarize**, AI will summarize in 2 lines.
10. If you type general text, AI will generate a response.
11. Use it for **career guidance** (“What skills for AI engineer?”).
12. Use it for **Q&A** (“What is blockchain in 2 lines?”).
13. Use it for **summarization** (“Summarize AI benefits in education”).
14. Use it for **decision help** (“AI or Cloud — which first?”).
15. You can type motivational requests (“I feel stressed”).
16. The agent is lightweight, runs free on Hugging Face.
17. No login required — runs directly in your browser.
18. Works best with short, clear sentences.
19. Try experimenting with multiple topics.
20. Share the app link with friends & colleagues 🚀.
---
"""
# Gradio UI with example + tutorial under output
with gr.Blocks() as demo:
gr.Markdown("# 🤖 Mini Agentic LLM App")
gr.Markdown("Smallest free demo of an Agentic AI using NLP + LLM on Hugging Face & Gradio.")
with gr.Row():
user_input = gr.Textbox(lines=3, placeholder="Type your text here...")
output = gr.Textbox(label="AI Output")
# Button
run_btn = gr.Button("🚀 Run")
# Always run with example at startup
demo.load(fn=agentic_ai, inputs=[gr.Textbox(value="Summarize AI in one line", visible=False)], outputs=output)
run_btn.click(agentic_ai, inputs=user_input, outputs=output)
# Tutorial under the app
gr.Markdown(tutorial_text)
if __name__ == "__main__":
demo.launch()