Spaces:
Sleeping
Sleeping
Rehman1603
commited on
Commit
•
2d12ada
1
Parent(s):
cb3d004
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import textwrap
|
4 |
+
|
5 |
+
# Load the question-answering pipeline
|
6 |
+
qa_pipeline = pipeline("question-answering")
|
7 |
+
|
8 |
+
def chatbot(document, question):
|
9 |
+
# Define a set of greeting phrases
|
10 |
+
greetings = ["hi", "hello", "hey", "greetings", "what's up", "howdy"]
|
11 |
+
|
12 |
+
# Check if the input question is a greeting
|
13 |
+
question_lower = question.lower().strip()
|
14 |
+
if question_lower in greetings or any(question_lower.startswith(greeting) for greeting in greetings):
|
15 |
+
return "Hello! How can I assist you with the document today?"
|
16 |
+
|
17 |
+
# Otherwise, handle the question using the QA pipeline
|
18 |
+
result = qa_pipeline(question=question, context=document)
|
19 |
+
|
20 |
+
# Wrap the answer to ensure it is 3 to 4 lines long
|
21 |
+
wrapped_answer = textwrap.fill(result['answer'], width=70)
|
22 |
+
|
23 |
+
# Split the wrapped answer into lines and limit it to 3 to 4 lines
|
24 |
+
answer_lines = wrapped_answer.split('\n')
|
25 |
+
limited_answer = '\n'.join(answer_lines[:4])
|
26 |
+
|
27 |
+
return limited_answer
|
28 |
+
|
29 |
+
interface = gr.Interface(
|
30 |
+
fn=chatbot,
|
31 |
+
inputs=[
|
32 |
+
gr.components.Textbox(lines=20, placeholder="Paste your document here..."),
|
33 |
+
gr.components.Textbox(lines=2, placeholder="Ask a question about the document or say hello...")
|
34 |
+
],
|
35 |
+
outputs="text",
|
36 |
+
title="Document Chatbot",
|
37 |
+
description="Upload a document and ask questions about its content or just say hello."
|
38 |
+
)
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
interface.launch(debug=True)
|