ayushsinha commited on
Commit
01fd84a
Β·
verified Β·
1 Parent(s): e97157f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +44 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, RobertaTokenizer, RobertaForQuestionAnswering
3
+ import torch
4
+
5
+ # Load the model and tokenizer
6
+ model_name = "AventIQ-AI/roberta-chatbot"
7
+ tokenizer = RobertaTokenizer.from_pretrained(model_name)
8
+ model = RobertaForQuestionAnswering.from_pretrained(model_name)
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+ model = model.to(device)
11
+
12
+ # Initialize the question-answering pipeline
13
+ qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
14
+
15
+ # Define the function for the Gradio interface
16
+ def roberta_chatbot(context, question):
17
+ if not context or not question:
18
+ return "Please provide both context and a question."
19
+
20
+ # Get the model's answer
21
+ result = qa_pipeline(question=question, context=context)
22
+ answer = result.get('answer', 'Sorry, I could not find an answer.')
23
+ return answer
24
+
25
+ # Create the Gradio interface
26
+ iface = gr.Interface(
27
+ fn=roberta_chatbot,
28
+ inputs=[
29
+ gr.Textbox(label="πŸ“„ Context", placeholder="Enter the context here...", lines=5),
30
+ gr.Textbox(label="❓ Question", placeholder="Enter your question here...", lines=2)
31
+ ],
32
+ outputs=gr.Textbox(label="πŸ€– Answer"),
33
+ title="🧠 RoBERTa-Powered Chatbot",
34
+ description="Provide a context and ask a question. The RoBERTa-based chatbot will find the answer based on the given context.",
35
+ examples=[
36
+ ["Flight AI101 departs from New York at 10:00 AM and arrives in San Francisco at 1:30 PM. The flight duration is 5 hours and 30 minutes.", "What is the duration of Flight AI101?"],
37
+ ["The Great Wall of China was built over several centuries to protect China's northern borders.", "Why was the Great Wall of China built?"]
38
+ ],
39
+ theme="compact",
40
+ allow_flagging="never"
41
+ )
42
+
43
+ if __name__ == "__main__":
44
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio
4
+ sentencepiece