File size: 1,465 Bytes
0db0383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

import gradio as gr
import joblib
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer

# Load the model
loaded_model = joblib.load("qa_model.joblib")
vectorizer = loaded_model["vectorizer"]
tfidf_matrix = loaded_model["tfidf_matrix"]
paragraphs = loaded_model["paragraphs"]

def answer_question(question):
    question_vector = vectorizer.transform([question])
    similarities = cosine_similarity(question_vector, tfidf_matrix)
    most_similar_paragraph_index = np.argmax(similarities)
    most_similar_paragraph = paragraphs[most_similar_paragraph_index]

    paragraph_sentences = most_similar_paragraph.split(".")
    best_sentence = ""
    max_overlap = 0

    question_words = set(question.lower().split())

    for sentence in paragraph_sentences:
        sentence = sentence.strip()
        if not sentence:
            continue
        sentence_words = set(sentence.lower().split())
        overlap = len(question_words.intersection(sentence_words))
        if overlap > max_overlap:
            max_overlap = overlap
            best_sentence = sentence

    return best_sentence.strip()

iface = gr.Interface(
    fn=answer_question,
    inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
    outputs="text",
    title="Mahabharata Question Answering",
    description="Ask a question about the Mahabharata, and the model will attempt to answer it.",
)

iface.launch()