khaledsayed1 commited on
Commit
1411a94
·
verified ·
1 Parent(s): deeb920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -28
app.py CHANGED
@@ -1,33 +1,61 @@
1
- from flask import Flask, request, jsonify
2
- from flask_cors import CORS
3
  from FINAL_CODE import answer_question
4
- app = Flask(__name__)
5
- CORS(app)
6
 
7
- @app.route('/api/ask', methods=['POST'])
8
- def ask_question():
 
 
9
  try:
10
- data = request.get_json()
11
- if not data or 'question' not in data:
12
- return jsonify({
13
- 'success': False,
14
- 'error': 'Question is required'
15
- }), 400
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 jsonify({
27
- 'success': False,
28
- 'error': str(e)
29
- }), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- if __name__ == '__main__':
32
- # Make sure to run with host='0.0.0.0' to be accessible externally if needed
33
- app.run(host='0.0.0.0', port=5000, debug=True)
 
 
 
 
 
 
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
+ )