IPL_Chatbot / app.py
ReddyYesh's picture
Update app.py
f4f9a0f verified
Raw History Blame Contribute Delete
1.8 kB
from flask import Flask, request, jsonify
import pandas as pd
import json
app = Flask(__name__)
# Load CSV data
csv_file_path = 'matches.csv'
data = pd.read_csv(csv_file_path)
@app.route('/webhook', methods=['POST'])
def webhook():
try:
data = request.get_json()
# Extract intent and query text
intent_name = data.get('queryResult', {}).get('intent', {}).get('displayName', '')
query_text = data.get('queryResult', {}).get('queryText', '')
if intent_name == 'GetMatchDetails':
response_text = get_match_details(query_text)
else:
response_text = f"Unhandled intent: {intent_name}"
except Exception as e:
response_text = f"An error occurred: {str(e)}"
return jsonify({"fulfillmentText": response_text})
def get_match_details(query_text):
# Search for a match by date or team
result = data[data['date'].str.contains(query_text, na=False) |
data['team1'].str.contains(query_text, na=False) |
data['team2'].str.contains(query_text, na=False)]
if not result.empty:
match_info = result.to_dict(orient='records')[0]
response_text = f"Match Details:\nDate: {match_info['date']}\n" \
f"Teams: {match_info['team1']} vs {match_info['team2']}\n" \
f"Toss Winner: {match_info['toss_winner']}\n" \
f"Winner: {match_info['winner']}\n" \
f"Player of the Match: {match_info['player_of_match']}\n" \
f"Venue: {match_info['venue']}"
else:
response_text = "No match found for the given query."
return response_text
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)