Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,61 @@
|
|
| 1 |
-
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
from FINAL_CODE import answer_question
|
| 4 |
-
app = Flask(__name__)
|
| 5 |
-
CORS(app)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
question = data['question']
|
| 18 |
-
answer = answer_question(question)
|
| 19 |
-
|
| 20 |
-
return jsonify({
|
| 21 |
-
'success': True,
|
| 22 |
-
'question': question,
|
| 23 |
-
'answer': answer
|
| 24 |
-
})
|
| 25 |
except Exception as e:
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
if __name__ ==
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from FINAL_CODE import answer_question
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def ask_question(question):
|
| 5 |
+
"""
|
| 6 |
+
Function to handle question answering with error handling
|
| 7 |
+
"""
|
| 8 |
try:
|
| 9 |
+
if not question or question.strip() == "":
|
| 10 |
+
return "Error: Question is required"
|
| 11 |
+
|
| 12 |
+
answer = answer_question(question.strip())
|
| 13 |
+
return answer
|
| 14 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
+
return f"Error: {str(e)}"
|
| 17 |
+
|
| 18 |
+
# Create Gradio interface
|
| 19 |
+
with gr.Blocks(title="Question Answering System") as demo:
|
| 20 |
+
gr.Markdown("# Question Answering System")
|
| 21 |
+
gr.Markdown("Ask any question and get an AI-powered answer!")
|
| 22 |
+
|
| 23 |
+
with gr.Row():
|
| 24 |
+
with gr.Column(scale=4):
|
| 25 |
+
question_input = gr.Textbox(
|
| 26 |
+
label="Your Question",
|
| 27 |
+
placeholder="Enter your question here...",
|
| 28 |
+
lines=3
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
with gr.Column(scale=1):
|
| 32 |
+
submit_btn = gr.Button("Ask Question", variant="primary")
|
| 33 |
+
|
| 34 |
+
answer_output = gr.Textbox(
|
| 35 |
+
label="Answer",
|
| 36 |
+
lines=10,
|
| 37 |
+
interactive=False
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Set up the interaction
|
| 41 |
+
submit_btn.click(
|
| 42 |
+
fn=ask_question,
|
| 43 |
+
inputs=question_input,
|
| 44 |
+
outputs=answer_output
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Also allow Enter key to submit
|
| 48 |
+
question_input.submit(
|
| 49 |
+
fn=ask_question,
|
| 50 |
+
inputs=question_input,
|
| 51 |
+
outputs=answer_output
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
# Launch the Gradio app
|
| 56 |
+
demo.launch(
|
| 57 |
+
server_name="0.0.0.0", # Equivalent to Flask's host='0.0.0.0'
|
| 58 |
+
server_port=7860, # Default Gradio port (you can change to 5000 if preferred)
|
| 59 |
+
share=False, # Set to True if you want a public link
|
| 60 |
+
debug=True
|
| 61 |
+
)
|