Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Required Libraries
|
| 2 |
+
import re
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from sentence_transformers import SentenceTransformer, util
|
| 6 |
+
|
| 7 |
+
# Load pre-trained models
|
| 8 |
+
qa_pipeline = pipeline("question-answering")
|
| 9 |
+
sentence_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
|
| 10 |
+
|
| 11 |
+
# Load the Chanakya Script
|
| 12 |
+
chanakya_text = """
|
| 13 |
+
BOOK I. Concerning Discipline.
|
| 14 |
+
The end of Sciences; association with the aged; restraint of
|
| 15 |
+
the organs of sense; the creation of ministers; the creation of
|
| 16 |
+
councillors and priests; ascertaining by temptations purity or
|
| 17 |
+
impurity in the character of ministers; the institution of spies.
|
| 18 |
+
Protection of parties for or against one's own cause in one's own
|
| 19 |
+
state; winning over the factions for or against an enemy's cause in
|
| 20 |
+
an enemy's state; the business of council meeting; the mission of
|
| 21 |
+
envoys; protection of princes; the conduct of a prince kept under
|
| 22 |
+
restraint; treatment of a prince kept under restraint; the duties of a
|
| 23 |
+
king; duty towards the harem; personal safety.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
# Preprocess Text
|
| 27 |
+
def preprocess_text(text):
|
| 28 |
+
sentences = re.split(r'[.;]', text) # Split into sentences
|
| 29 |
+
return [sentence.strip() for sentence in sentences if sentence.strip()]
|
| 30 |
+
|
| 31 |
+
# Embed the sentences for similarity matching
|
| 32 |
+
def get_embeddings(sentences):
|
| 33 |
+
return sentence_model.encode(sentences, convert_to_tensor=True)
|
| 34 |
+
|
| 35 |
+
# Match Query with Closest Text
|
| 36 |
+
def get_closest_text(query, sentences, embeddings):
|
| 37 |
+
query_embedding = sentence_model.encode(query, convert_to_tensor=True)
|
| 38 |
+
scores = util.pytorch_cos_sim(query_embedding, embeddings)
|
| 39 |
+
closest_idx = scores.argmax().item()
|
| 40 |
+
return sentences[closest_idx]
|
| 41 |
+
|
| 42 |
+
# Preprocess and embed the script
|
| 43 |
+
sentences = preprocess_text(chanakya_text)
|
| 44 |
+
embeddings = get_embeddings(sentences)
|
| 45 |
+
|
| 46 |
+
# Streamlit App
|
| 47 |
+
st.title("Chanakya GPT")
|
| 48 |
+
st.write("Ask questions about Chanakya's teachings!")
|
| 49 |
+
|
| 50 |
+
user_query = st.text_input("Enter your question:")
|
| 51 |
+
if user_query:
|
| 52 |
+
closest_sentence = get_closest_text(user_query, sentences, embeddings)
|
| 53 |
+
st.write(f"**Chanakya Says:** {closest_sentence}")
|
| 54 |
+
|
| 55 |
+
# Using Hugging Face QA model to refine the answer
|
| 56 |
+
answer = qa_pipeline(question=user_query, context=closest_sentence)
|
| 57 |
+
st.write(f"**Refined Answer:** {answer['answer']}")
|