prasenjeet099 commited on
Commit
0db0383
·
verified ·
1 Parent(s): ace0e9f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import joblib
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ from sklearn.feature_extraction.text import TfidfVectorizer
6
+
7
+ # Load the model
8
+ loaded_model = joblib.load("qa_model.joblib")
9
+ vectorizer = loaded_model["vectorizer"]
10
+ tfidf_matrix = loaded_model["tfidf_matrix"]
11
+ paragraphs = loaded_model["paragraphs"]
12
+
13
+ def answer_question(question):
14
+ question_vector = vectorizer.transform([question])
15
+ similarities = cosine_similarity(question_vector, tfidf_matrix)
16
+ most_similar_paragraph_index = np.argmax(similarities)
17
+ most_similar_paragraph = paragraphs[most_similar_paragraph_index]
18
+
19
+ paragraph_sentences = most_similar_paragraph.split(".")
20
+ best_sentence = ""
21
+ max_overlap = 0
22
+
23
+ question_words = set(question.lower().split())
24
+
25
+ for sentence in paragraph_sentences:
26
+ sentence = sentence.strip()
27
+ if not sentence:
28
+ continue
29
+ sentence_words = set(sentence.lower().split())
30
+ overlap = len(question_words.intersection(sentence_words))
31
+ if overlap > max_overlap:
32
+ max_overlap = overlap
33
+ best_sentence = sentence
34
+
35
+ return best_sentence.strip()
36
+
37
+ iface = gr.Interface(
38
+ fn=answer_question,
39
+ inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
40
+ outputs="text",
41
+ title="Mahabharata Question Answering",
42
+ description="Ask a question about the Mahabharata, and the model will attempt to answer it.",
43
+ )
44
+
45
+ iface.launch()