Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Simple knowledge base
|
| 4 |
+
knowledge = {
|
| 5 |
+
"ai": "Artificial Intelligence (AI) is machines that can perform tasks requiring human intelligence.",
|
| 6 |
+
"ml": "Machine Learning (ML) is a subset of AI where algorithms learn from data.",
|
| 7 |
+
"llm": "Large Language Models (LLMs) are AI systems trained on vast text data.",
|
| 8 |
+
"rag": "RAG (Retrieval Augmented Generation) combines document retrieval with AI generation.",
|
| 9 |
+
"hugging face": "Hugging Face is a platform for sharing AI models and datasets.",
|
| 10 |
+
"transformers": "Transformers are neural network architectures used in modern AI models."
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def answer_question(question):
|
| 14 |
+
"""Simple keyword-based answering"""
|
| 15 |
+
question_lower = question.lower()
|
| 16 |
+
|
| 17 |
+
# Check for keywords
|
| 18 |
+
for keyword, answer in knowledge.items():
|
| 19 |
+
if keyword in question_lower:
|
| 20 |
+
return f"**{keyword.upper()}**: {answer}"
|
| 21 |
+
|
| 22 |
+
# Default response
|
| 23 |
+
return """I'm a simple Q&A agent. I know about:
|
| 24 |
+
• AI (Artificial Intelligence)
|
| 25 |
+
• ML (Machine Learning)
|
| 26 |
+
• LLM (Large Language Models)
|
| 27 |
+
• RAG (Retrieval Augmented Generation)
|
| 28 |
+
• Hugging Face
|
| 29 |
+
• Transformers
|
| 30 |
+
|
| 31 |
+
Try asking about any of these topics!"""
|
| 32 |
+
|
| 33 |
+
# Create interface
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# 🤖 Simple Q&A Agent")
|
| 36 |
+
gr.Markdown("Ask me about AI, ML, LLMs, RAG, Hugging Face, or Transformers")
|
| 37 |
+
|
| 38 |
+
# Input and output
|
| 39 |
+
question_input = gr.Textbox(
|
| 40 |
+
label="Your Question",
|
| 41 |
+
placeholder="e.g., What is AI?",
|
| 42 |
+
lines=2
|
| 43 |
+
)
|
| 44 |
+
answer_output = gr.Textbox(
|
| 45 |
+
label="Answer",
|
| 46 |
+
lines=5
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Button
|
| 50 |
+
submit_btn = gr.Button("Get Answer", variant="primary")
|
| 51 |
+
|
| 52 |
+
# Function
|
| 53 |
+
def get_answer(q):
|
| 54 |
+
return answer_question(q)
|
| 55 |
+
|
| 56 |
+
# Connect
|
| 57 |
+
submit_btn.click(get_answer, inputs=[question_input], outputs=[answer_output])
|
| 58 |
+
question_input.submit(get_answer, inputs=[question_input], outputs=[answer_output])
|
| 59 |
+
|
| 60 |
+
# Examples
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=[
|
| 63 |
+
"What is AI?",
|
| 64 |
+
"Explain machine learning",
|
| 65 |
+
"What are LLMs?",
|
| 66 |
+
"How does RAG work?",
|
| 67 |
+
"What is Hugging Face?"
|
| 68 |
+
],
|
| 69 |
+
inputs=[question_input]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch
|
| 73 |
+
demo.launch(debug=False)
|