ReddyYesh commited on
Commit
68c10f7
·
verified ·
1 Parent(s): 53078da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -43
app.py CHANGED
@@ -1,35 +1,38 @@
1
- import streamlit as st
 
2
  import pandas as pd
3
  import requests
4
  import json
5
 
 
 
6
  # Load CSV data
7
- @st.cache
8
- def load_data():
9
- csv_file_path = 'matches.csv'
10
- data = pd.read_csv(csv_file_path)
11
- return data
12
 
13
- data = load_data()
 
 
 
 
14
 
15
- # Define the function to interact with Dialogflow
16
- def send_to_dialogflow(query_text):
17
- headers = {
18
- 'Authorization': 'Bearer your-dialogflow-api-key',
19
- 'Content-Type': 'application/json'
20
- }
 
 
 
21
 
22
- data = {
23
- "queryInput": {
24
- "text": {
25
- "text": query_text,
26
- "languageCode": "en"
27
- }
28
- }
29
- }
30
 
31
- response = requests.post(DIALOGFLOW_ENDPOINT, headers=headers, data=json.dumps(data))
32
- return response.json()
33
 
34
  def get_match_details(query_text):
35
  # Search for a match by date or team
@@ -50,23 +53,4 @@ def get_match_details(query_text):
50
 
51
  return response_text
52
 
53
- # Streamlit app interface
54
- st.title("Match Details Query Interface")
55
-
56
- query_text = st.text_input("Enter your query:")
57
-
58
- if st.button("Send"):
59
- if query_text:
60
- dialogflow_response = send_to_dialogflow(query_text)
61
-
62
- intent_name = dialogflow_response['queryResult']['intent']['displayName']
63
- fulfillment_text = dialogflow_response['queryResult']['fulfillmentText']
64
-
65
- if intent_name == 'GetMatchDetails':
66
- response_text = get_match_details(query_text)
67
- else:
68
- response_text = f"Unhandled intent: {intent_name}"
69
-
70
- st.write(response_text)
71
- else:
72
- st.error("Please enter a query.")
 
1
+ from fastapi import FastAPI, HTTPException, Request
2
+ from pydantic import BaseModel
3
  import pandas as pd
4
  import requests
5
  import json
6
 
7
+ app = FastAPI()
8
+
9
  # Load CSV data
10
+ csv_file_path = 'C:/Users/USER/Desktop/matches.csv'
11
+ data = pd.read_csv(csv_file_path)
12
+
13
+ class DialogflowRequest(BaseModel):
14
+ queryInput: dict
15
 
16
+ @app.post("/webhook")
17
+ async def webhook(request: Request):
18
+ try:
19
+ data = await request.json()
20
+ dialogflow_request = DialogflowRequest(**data)
21
 
22
+ # Extract intent and query text
23
+ query_text = dialogflow_request.queryInput['text']['text']
24
+ intent_name = dialogflow_request.queryInput.get('intent', {}).get('displayName', '')
25
+
26
+ # Call the function to handle the query
27
+ if intent_name == 'GetMatchDetails':
28
+ response_text = get_match_details(query_text)
29
+ else:
30
+ response_text = f"Unhandled intent: {intent_name}"
31
 
32
+ except Exception as e:
33
+ response_text = f"An error occurred: {str(e)}"
 
 
 
 
 
 
34
 
35
+ return {"fulfillmentText": response_text}
 
36
 
37
  def get_match_details(query_text):
38
  # Search for a match by date or team
 
53
 
54
  return response_text
55
 
56
+ # Run the application with: uvicorn main:app --reload