MFF212 commited on
Commit
c8e0891
·
1 Parent(s): ba970f2

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -72
main.py CHANGED
@@ -1,56 +1,23 @@
1
- from fastapi import FastAPI, UploadFile, File
2
- from fastapi.responses import HTMLResponse
3
- from fastapi import FastAPI
4
- import json
5
- import requests
6
  import csv
7
- from fastapi import FastAPI, UploadFile, File
8
- from fastapi.responses import HTMLResponse
9
- from fastapi.responses import HTMLResponse, FileResponse
 
 
10
  # Dictionaries to store URLs, API counts, and associated names
11
  url_counts = {}
12
  api_counts = {}
13
  url_names = {}
14
-
15
- app = FastAPI()
16
-
17
- def ask(query):
18
- headers = {"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiM2NlMTljZDItNTk2Zi00NzRhLWE3OGMtZWZhNTM1YWJlNmY0IiwidHlwZSI6ImFwaV90b2tlbiJ9.x_PPBluoOy2GT3ZjDQ3dcS8WtbNn95RfkoFKkhM_a5A"} # Replace with your API key
19
- url = "https://api.edenai.run/v2/text/chat"
20
-
21
- # Create the content dictionary
22
- content ="The inventory consists of various items, including Milk (50 units in stock, 10 units pending), Bread (100 units in stock, 20 units pending), Eggs (75 dozen in stock, 5 dozen pending), Rice (200 pounds in stock, 15 pounds pending), Chicken (40 pounds in stock, 8 pounds pending), Fresh Vegetables (various in stock, 30 pounds pending), Canned Soup (150 cans in stock, 25 cans pending), Pasta (80 pounds in stock, 10 pounds pending), Toilet Paper (60 rolls in stock, 12 rolls pending), and Shampoo (30 bottles in stock, 5 bottles pending)."
23
- # Concatenate the input query with the "text" field in the payload
24
- payload = {
25
- "providers": "openai",
26
- "text": "from the summary: ",
27
- "chatbot_global_action": content + " " + query,
28
- "previous_history": [],
29
- "temperature": 0.0,
30
- "max_tokens": 300
31
- }
32
-
33
- response = requests.post(url, json=payload, headers=headers)
34
- print(response)
35
- result = json.loads(response.text)
36
- print(result)
37
- generated_text = result['openai']['generated_text']
38
- print(generated_text)
39
- return generated_text
40
-
41
- @app.get("/ask")
42
- async def ask_question(question_text: str):
43
- # Call the question function with the provided question_text
44
- response = ask(question_text)
45
- return {"response": response}
46
 
47
  @app.post("/upload/")
48
- async def upload_file(file: UploadFile = File(...)):
49
- contents = await file.read()
50
- with open(contents, 'r', newline='', encoding='utf-8') as csvfile:
 
51
  csvreader = csv.reader(csvfile)
52
  header = next(csvreader) # Skip the header row
53
- total_records = 0
54
  for row in csvreader:
55
  url = row[3] # Assuming the URL is in the 4th column (index 3)
56
  api = row[4] # Assuming the API is in the 5th column (index 4)
@@ -76,31 +43,20 @@ async def upload_file(file: UploadFile = File(...)):
76
  redundant_urls = [url for url, count in url_counts.items() if count > 1]
77
  redundant_apis = [api for api, count in api_counts.items() if count > 1]
78
 
79
- # Print the results including names
80
- print("Total GET Records:", total_records)
81
-
82
- print("\nRedundant GET Requests:")
83
- for url in redundant_urls:
84
- print("URL:", url)
85
- print("Name:", url_names[url])
86
- print("Count:", url_counts[url])
87
- print("---")
88
-
89
- # Calculate the percentage of redundant URLs and APIs
90
- percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0
91
-
92
- print("\nPercentage of Redundant GET Requests: {:.2f}%".format(percentage_redundant_urls))
93
-
94
-
95
-
96
-
97
-
98
- return HTMLResponse(content=percentage_redundant_urls)
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
 
1
+ from fastapi import FastAPI, File, UploadFile
 
 
 
 
2
  import csv
3
+ from typing import List
4
+ from io import TextIOWrapper
5
+
6
+ app = FastAPI()
7
+
8
  # Dictionaries to store URLs, API counts, and associated names
9
  url_counts = {}
10
  api_counts = {}
11
  url_names = {}
12
+ api_names = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  @app.post("/upload/")
15
+ async def upload_csv(file: UploadFile):
16
+ if file.content_type == "text/csv":
17
+ total_records = 0
18
+ csvfile = TextIOWrapper(file.file, encoding='utf-8')
19
  csvreader = csv.reader(csvfile)
20
  header = next(csvreader) # Skip the header row
 
21
  for row in csvreader:
22
  url = row[3] # Assuming the URL is in the 4th column (index 3)
23
  api = row[4] # Assuming the API is in the 5th column (index 4)
 
43
  redundant_urls = [url for url, count in url_counts.items() if count > 1]
44
  redundant_apis = [api for api, count in api_counts.items() if count > 1]
45
 
46
+ # Calculate the percentage of redundant URLs and APIs
47
+ percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0
48
+
49
+ return {
50
+ "Total GET Records": total_records,
51
+ "Redundant GET Requests": [
52
+ {
53
+ "URL": url,
54
+ "Name": url_names[url],
55
+ "Count": url_counts[url]
56
+ }
57
+ for url in redundant_urls
58
+ ],
59
+ "Percentage of Redundant GET Requests": round(percentage_redundant_urls, 2)
60
+ }
61
+ else:
62
+ return {"error": "Invalid file format. Please upload a CSV file."}