Spaces:
Sleeping
Sleeping
File size: 1,797 Bytes
f4f9a0f bc40f7e f4f9a0f d8a4f79 f4f9a0f 68c10f7 f4f9a0f 8383fcc 68c10f7 f4f9a0f 68c10f7 f4f9a0f bcd9574 68c10f7 f4f9a0f bcd9574 68c10f7 d8a4f79 68c10f7 d8a4f79 f4f9a0f d8a4f79 bc40f7e d8a4f79 f4f9a0f 8383fcc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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)
|