Update app.py
Browse files
app.py
CHANGED
|
@@ -2,88 +2,56 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import faiss
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
-
from transformers import RagTokenizer, RagSequenceForGeneration
|
| 6 |
|
| 7 |
-
# 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
custom_knowledge = [
|
| 9 |
-
# Professional Identity
|
| 10 |
"Aderaw Molla is an ERP and Backend Developer based in Addis Ababa, Ethiopia.",
|
| 11 |
-
|
| 12 |
-
"He holds a B.Sc in Software Engineering from Bahirdar University with a 3.9/4.0 GPA.",
|
| 13 |
-
|
| 14 |
-
# Current Roles
|
| 15 |
-
"At ERP Solutions Ethiopia, Aderaw customizes modules for HR, Payroll, Inventory, and Finance since February 2023.",
|
| 16 |
-
"At Devssinia Software, he develops backend systems using Django and Hasura since May 2023.",
|
| 17 |
-
|
| 18 |
-
# Technical Skills
|
| 19 |
-
"Programming Languages: Python (Expert), JavaScript (Advanced), Go (Intermediate).",
|
| 20 |
-
"Frameworks: Django, Node.js, Express.js, Frappe, Gin Framework.",
|
| 21 |
-
"Databases: PostgreSQL, MariaDB, Firebase.",
|
| 22 |
-
"DevOps: Docker, AWS EC2/S3, Azure DevOps.",
|
| 23 |
-
|
| 24 |
-
# Projects
|
| 25 |
-
"African Gypsum ERP: Customized Sales, Procurement, Stock, HR, Payroll and Manufacturing modules.",
|
| 26 |
-
"ARRCA ERP: Trained 50 project managers and 3000 users across multiple sites.",
|
| 27 |
-
"Health Management System: Built for Kidane Mihret Clinic using React and Node.js.",
|
| 28 |
-
"Agriconnect: A Django/Flutter app connecting farmers with merchants.",
|
| 29 |
-
|
| 30 |
-
# Contact Info
|
| 31 |
-
"Email: mollaaderaw@gmail.com | Phone: +251-921-05-06-99",
|
| 32 |
-
"LinkedIn: linkedin.com/in/aderaw-molla-31b23623b",
|
| 33 |
-
"GitHub: github.com/aderawmolla",
|
| 34 |
-
|
| 35 |
-
# Education
|
| 36 |
-
"Bahirdar University (2019-2024): B.Sc in Software Engineering, GPA 3.9/4.0.",
|
| 37 |
-
"Certifications: Practical Machine Learning, Big Data Analytics, Fullstack Development.",
|
| 38 |
-
|
| 39 |
-
# Additional
|
| 40 |
-
"Languages: Fluent in English and Amharic (Native).",
|
| 41 |
-
"Specialties: API Design, Cloud Architecture, ERP Automation.",
|
| 42 |
-
"Recent Training: Conducted ERP training for 3000+ users at ARRCA."
|
| 43 |
]
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
|
| 47 |
-
model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-nq")
|
| 48 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 49 |
-
|
| 50 |
-
# 3. FAISS Index with Lower Similarity Threshold (0.5)
|
| 51 |
knowledge_embeddings = embedder.encode(custom_knowledge)
|
| 52 |
index = faiss.IndexFlatL2(knowledge_embeddings.shape[1])
|
| 53 |
index.add(knowledge_embeddings)
|
| 54 |
|
| 55 |
def answer_question(question):
|
| 56 |
-
#
|
| 57 |
query_embed = embedder.encode([question])
|
| 58 |
distances, indices = index.search(query_embed, k=1)
|
| 59 |
-
similarity = 1 - distances[0][0] / 2
|
| 60 |
|
| 61 |
-
if similarity > 0.5:
|
| 62 |
-
return custom_knowledge[indices[0][0]]
|
| 63 |
|
| 64 |
-
#
|
| 65 |
inputs = tokenizer(question, return_tensors="pt")
|
| 66 |
outputs = model.generate(
|
| 67 |
input_ids=inputs["input_ids"],
|
|
|
|
| 68 |
max_length=100
|
| 69 |
)
|
| 70 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 71 |
|
| 72 |
-
# 4. Gradio Interface
|
| 73 |
-
examples = [
|
| 74 |
-
"What is Aderaw's current position?",
|
| 75 |
-
"List Aderaw's technical skills",
|
| 76 |
-
"Where did Aderaw study?",
|
| 77 |
-
"How can I contact Aderaw?",
|
| 78 |
-
"What ERP projects has Aderaw worked on?"
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
iface = gr.Interface(
|
| 82 |
fn=answer_question,
|
| 83 |
inputs="text",
|
| 84 |
outputs="text",
|
| 85 |
-
examples=
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
iface.launch(share=True
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import faiss
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
|
| 6 |
|
| 7 |
+
# 1. Initialize with Dummy Retriever (REQUIRED for RAG)
|
| 8 |
+
tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
|
| 9 |
+
retriever = RagRetriever.from_pretrained(
|
| 10 |
+
"facebook/rag-sequence-nq",
|
| 11 |
+
index_name="exact",
|
| 12 |
+
use_dummy_dataset=True # Critical for Hugging Face Spaces
|
| 13 |
+
)
|
| 14 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-nq", retriever=retriever)
|
| 15 |
+
|
| 16 |
+
# 2. Custom Knowledge Base
|
| 17 |
custom_knowledge = [
|
|
|
|
| 18 |
"Aderaw Molla is an ERP and Backend Developer based in Addis Ababa, Ethiopia.",
|
| 19 |
+
# ... (rest of your knowledge base)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
+
# 3. FAISS Setup for Custom Knowledge
|
|
|
|
|
|
|
| 23 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
|
|
|
| 24 |
knowledge_embeddings = embedder.encode(custom_knowledge)
|
| 25 |
index = faiss.IndexFlatL2(knowledge_embeddings.shape[1])
|
| 26 |
index.add(knowledge_embeddings)
|
| 27 |
|
| 28 |
def answer_question(question):
|
| 29 |
+
# First check custom knowledge
|
| 30 |
query_embed = embedder.encode([question])
|
| 31 |
distances, indices = index.search(query_embed, k=1)
|
| 32 |
+
similarity = 1 - distances[0][0] / 2
|
| 33 |
|
| 34 |
+
if similarity > 0.5:
|
| 35 |
+
return custom_knowledge[indices[0][0]]
|
| 36 |
|
| 37 |
+
# Then use RAG with proper retriever setup
|
| 38 |
inputs = tokenizer(question, return_tensors="pt")
|
| 39 |
outputs = model.generate(
|
| 40 |
input_ids=inputs["input_ids"],
|
| 41 |
+
attention_mask=inputs["attention_mask"], # Required
|
| 42 |
max_length=100
|
| 43 |
)
|
| 44 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
|
| 46 |
+
# 4. Gradio Interface (NO share=True needed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=answer_question,
|
| 49 |
inputs="text",
|
| 50 |
outputs="text",
|
| 51 |
+
examples=[
|
| 52 |
+
"What is Aderaw's current position?",
|
| 53 |
+
"List Aderaw's technical skills"
|
| 54 |
+
],
|
| 55 |
+
title="Aderaw Molla - Professional Q&A"
|
| 56 |
)
|
| 57 |
+
iface.launch() # Remove share=True for Spaces
|