Spaces:
Sleeping
Sleeping
ajaynagotha
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,65 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer,
|
|
|
|
|
|
|
3 |
|
|
|
4 |
model_name = "distilbert/distilbert-base-cased-distilled-squad"
|
5 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
6 |
-
model =
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
Key teachings include performing one's duty without attachment to results, the importance of self-realization, and the path to liberation (Moksha).
|
18 |
-
"""
|
19 |
|
20 |
def generate_response(question):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
|
27 |
iface = gr.Interface(
|
28 |
-
fn=
|
29 |
inputs=gr.Textbox(lines=2, placeholder="Enter your question about the Bhagavad Gita here..."),
|
30 |
-
outputs="text"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
)
|
32 |
|
33 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
3 |
+
import torch
|
4 |
+
from mlcroissant import Dataset
|
5 |
+
import random
|
6 |
|
7 |
+
# Load the DistilBERT model and tokenizer
|
8 |
model_name = "distilbert/distilbert-base-cased-distilled-squad"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Load the Bhagavad Gita dataset
|
13 |
+
ds = Dataset(jsonld="https://huggingface.co/api/datasets/knowrohit07/gita_dataset/croissant")
|
14 |
+
records = list(ds.records("default"))
|
15 |
+
|
16 |
+
def get_relevant_context(question):
|
17 |
+
# Randomly select 5 records to form the context
|
18 |
+
selected_records = random.sample(records, 5)
|
19 |
+
context = " ".join([record["Text"] for record in selected_records])
|
20 |
+
return context
|
|
|
|
|
21 |
|
22 |
def generate_response(question):
|
23 |
+
context = get_relevant_context(question)
|
24 |
+
|
25 |
+
# Encode the question and context
|
26 |
+
inputs = tokenizer.encode_plus(question, context, add_special_tokens=True, return_tensors="pt")
|
27 |
+
input_ids = inputs["input_ids"].tolist()[0]
|
28 |
+
|
29 |
+
# Get the answer
|
30 |
+
outputs = model(**inputs)
|
31 |
+
answer_start = torch.argmax(outputs.start_logits)
|
32 |
+
answer_end = torch.argmax(outputs.end_logits) + 1
|
33 |
+
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
|
34 |
+
|
35 |
+
# If the model couldn't find an answer, provide a default response
|
36 |
+
if answer == "" or answer == "[CLS]" or answer == "[SEP]":
|
37 |
+
answer = "I'm sorry, but I couldn't find a specific answer to that question in the Bhagavad Gita. Could you please rephrase your question or ask about a different topic from the Gita?"
|
38 |
+
|
39 |
+
# Add a disclaimer
|
40 |
+
disclaimer = "\n\nPlease note: This response is generated by an AI model based on the Bhagavad Gita dataset. For authoritative information, please consult the original text or scholarly sources."
|
41 |
+
|
42 |
+
return answer + disclaimer
|
43 |
+
|
44 |
+
# Define the predict function for the API
|
45 |
+
def predict(question):
|
46 |
+
return generate_response(question)
|
47 |
|
48 |
+
# Create the Gradio interface
|
49 |
iface = gr.Interface(
|
50 |
+
fn=predict,
|
51 |
inputs=gr.Textbox(lines=2, placeholder="Enter your question about the Bhagavad Gita here..."),
|
52 |
+
outputs="text",
|
53 |
+
title="Bhagavad Gita Q&A Assistant",
|
54 |
+
description="Ask questions about the Bhagavad Gita. The AI will attempt to provide answers based on the text.",
|
55 |
+
examples=[
|
56 |
+
["What is the main message of the Bhagavad Gita?"],
|
57 |
+
["Who is Krishna in the Bhagavad Gita?"],
|
58 |
+
["What does the Gita say about dharma?"],
|
59 |
+
["How does the Bhagavad Gita define yoga?"],
|
60 |
+
["What is the significance of Arjuna's dilemma?"]
|
61 |
+
]
|
62 |
)
|
63 |
|
64 |
+
# Launch the interface with sharing enabled
|
65 |
+
iface.launch(share=True)
|