GermanySutherland commited on
Commit
dddfb60
·
verified ·
1 Parent(s): 33983d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load a free Hugging Face model (small + free to run)
5
+ generator = pipeline("text2text-generation", model="google/flan-t5-small")
6
+
7
+ # Agent function
8
+ def agentic_ai(user_input):
9
+ # Step 1: Analyze input
10
+ analysis_prompt = f"Analyze the intent of this input: {user_input}"
11
+ analysis = generator(analysis_prompt, max_length=50, do_sample=False)[0]['generated_text']
12
+
13
+ # Step 2: Decide what to do (simple rule-based agent)
14
+ if "summarize" in user_input.lower():
15
+ task_prompt = f"Summarize this text in 2 lines: {user_input}"
16
+ elif "question" in user_input.lower() or "?" in user_input:
17
+ task_prompt = f"Answer this question briefly: {user_input}"
18
+ else:
19
+ task_prompt = f"Generate a helpful response: {user_input}"
20
+
21
+ # Step 3: LLM Response
22
+ response = generator(task_prompt, max_length=80, do_sample=False)[0]['generated_text']
23
+
24
+ # Step 4: Return both analysis + final response
25
+ return f"🔎 Agent Analysis: {analysis}\n\n💡 Agent Response: {response}"
26
+
27
+
28
+ # Tutorial text (20 bullets)
29
+ tutorial_text = """
30
+ ---
31
+
32
+ ## 📘 How to Use this Web App (Step by Step)
33
+
34
+ 1. Open the app on Hugging Face.
35
+ 2. You’ll see a text box in the center.
36
+ 3. Type your question, statement, or text.
37
+ 4. Click the orange **Submit** button.
38
+ 5. The AI will analyze your input first.
39
+ 6. You will see **Agent Analysis** (what AI thinks you mean).
40
+ 7. You will see **Agent Response** (the final helpful reply).
41
+ 8. If you ask a question (with “?”), AI gives an answer.
42
+ 9. If you ask to **summarize**, AI will summarize in 2 lines.
43
+ 10. If you type general text, AI will generate a response.
44
+ 11. Use it for **career guidance** (“What skills for AI engineer?”).
45
+ 12. Use it for **Q&A** (“What is blockchain in 2 lines?”).
46
+ 13. Use it for **summarization** (“Summarize AI benefits in education”).
47
+ 14. Use it for **decision help** (“AI or Cloud — which first?”).
48
+ 15. You can type motivational requests (“I feel stressed”).
49
+ 16. The agent is lightweight, runs free on Hugging Face.
50
+ 17. No login required — runs directly in your browser.
51
+ 18. Works best with short, clear sentences.
52
+ 19. Try experimenting with multiple topics.
53
+ 20. Share the app link with friends & colleagues 🚀.
54
+
55
+ ---
56
+ """
57
+
58
+ # Gradio UI with example + tutorial under output
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# 🤖 Mini Agentic LLM App")
61
+ gr.Markdown("Smallest free demo of an Agentic AI using NLP + LLM on Hugging Face & Gradio.")
62
+
63
+ with gr.Row():
64
+ user_input = gr.Textbox(lines=3, placeholder="Type your text here...")
65
+ output = gr.Textbox(label="AI Output")
66
+
67
+ # Button
68
+ run_btn = gr.Button("🚀 Run")
69
+
70
+ # Always run with example at startup
71
+ demo.load(fn=agentic_ai, inputs=[gr.Textbox(value="Summarize AI in one line", visible=False)], outputs=output)
72
+
73
+ run_btn.click(agentic_ai, inputs=user_input, outputs=output)
74
+
75
+ # Tutorial under the app
76
+ gr.Markdown(tutorial_text)
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()