ajaynagotha commited on
Commit
3330dd3
·
verified ·
1 Parent(s): d89045d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -22
app.py CHANGED
@@ -1,33 +1,65 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
 
3
 
 
4
  model_name = "distilbert/distilbert-base-cased-distilled-squad"
5
  tokenizer = AutoTokenizer.from_pretrained(model_name)
6
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
7
-
8
- gita_context = """
9
- The Bhagavad Gita is a 700-verse Hindu scripture that is part of the Indian epic Mahabharata. It is a dialogue between Prince Arjuna and Lord Krishna, who serves as his charioteer. The Gita's core message includes:
10
- 1. The immortality of the soul (Atman)
11
- 2. The nature of action (Karma) and duty (Dharma)
12
- 3. The importance of devotion (Bhakti)
13
- 4. The pursuit of knowledge (Jnana) and wisdom
14
- 5. Different types of Yoga: Karma Yoga, Bhakti Yoga, Jnana Yoga, and Raja Yoga
15
- 6. The concept of detachment from the fruits of one's actions
16
- 7. The divine nature of Krishna as an avatar of Vishnu
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
- prompt = f"Based on the following context about the Bhagavad Gita, answer the question.\n\nContext: {gita_context}\n\nQuestion: {question}\n\nAnswer:"
22
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids
23
- outputs = model.generate(input_ids, max_new_tokens=200, do_sample=True, temperature=0.7, top_p=0.95)
24
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
 
27
  iface = gr.Interface(
28
- fn=generate_response,
29
  inputs=gr.Textbox(lines=2, placeholder="Enter your question about the Bhagavad Gita here..."),
30
- outputs="text"
 
 
 
 
 
 
 
 
 
31
  )
32
 
33
- iface.launch()
 
 
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)