Rakshitjan commited on
Commit
9f94679
·
verified ·
1 Parent(s): 2365bbf

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -18
main.py CHANGED
@@ -1,7 +1,12 @@
1
- import gspread
 
2
  from google.oauth2.service_account import Credentials
 
3
  from google.auth.exceptions import GoogleAuthError
4
  import os
 
 
 
5
 
6
  # Define chapter categories
7
  physics_chapters = {
@@ -29,7 +34,6 @@ maths_chapters = {
29
  "three dimensional geometry", "vector algebra", "statistics and probability", "trigonometry"
30
  }
31
 
32
- # Define base class for chapters
33
  class Chapter:
34
  def __init__(self):
35
  self.data = {}
@@ -41,12 +45,13 @@ class Chapter:
41
  self.data[chapter_name]["count"] += 1
42
 
43
  def print_averages(self):
 
44
  for chapter, stats in self.data.items():
45
  if stats["count"] > 0:
46
  avg = stats["sum_marks"] / stats["count"]
47
- print(f"{chapter}: Average = {round(avg, 1)}")
 
48
 
49
- # Create specific classes for Physics, Chemistry, and Mathematics
50
  class PhysicsChapter(Chapter):
51
  pass
52
 
@@ -56,7 +61,6 @@ class ChemistryChapter(Chapter):
56
  class MathsChapter(Chapter):
57
  pass
58
 
59
- # Function to fetch the credentials from environment variables or manually inputted credentials in Colab
60
  def get_credentials_from_env():
61
  service_account_info = {
62
  "type": os.getenv("SERVICE_ACCOUNT_TYPE"),
@@ -76,7 +80,7 @@ def get_credentials_from_env():
76
  creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
77
  return creds
78
 
79
- # Main function to analyze chapter averages
80
  def analyze_chapter_averages():
81
  try:
82
  # Set up credentials
@@ -118,20 +122,19 @@ def analyze_chapter_averages():
118
 
119
  i += 2 # Move to the next chapter and marks
120
 
121
- # Print average marks per chapter for each subject
122
- print("Physics Chapters:")
123
- physics_tracker.print_averages()
124
- print("\nChemistry Chapters:")
125
- chemistry_tracker.print_averages()
126
- print("\nMaths Chapters:")
127
- maths_tracker.print_averages()
128
 
129
  except GoogleAuthError as e:
130
- print(f"Authentication error: {e}")
131
  except gspread.exceptions.SpreadsheetNotFound:
132
- print("Spreadsheet not found. Please check the URL.")
133
  except Exception as e:
134
- print(f"An error occurred: {e}")
135
 
136
- # Call the main function
137
- analyze_chapter_averages()
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
  from google.oauth2.service_account import Credentials
4
+ import gspread
5
  from google.auth.exceptions import GoogleAuthError
6
  import os
7
+ import uvicorn
8
+
9
+ app = FastAPI()
10
 
11
  # Define chapter categories
12
  physics_chapters = {
 
34
  "three dimensional geometry", "vector algebra", "statistics and probability", "trigonometry"
35
  }
36
 
 
37
  class Chapter:
38
  def __init__(self):
39
  self.data = {}
 
45
  self.data[chapter_name]["count"] += 1
46
 
47
  def print_averages(self):
48
+ result = {}
49
  for chapter, stats in self.data.items():
50
  if stats["count"] > 0:
51
  avg = stats["sum_marks"] / stats["count"]
52
+ result[chapter] = round(avg, 1)
53
+ return result
54
 
 
55
  class PhysicsChapter(Chapter):
56
  pass
57
 
 
61
  class MathsChapter(Chapter):
62
  pass
63
 
 
64
  def get_credentials_from_env():
65
  service_account_info = {
66
  "type": os.getenv("SERVICE_ACCOUNT_TYPE"),
 
80
  creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
81
  return creds
82
 
83
+ @app.get("/analyze_chapter_averages")
84
  def analyze_chapter_averages():
85
  try:
86
  # Set up credentials
 
122
 
123
  i += 2 # Move to the next chapter and marks
124
 
125
+ # Return average marks per chapter for each subject
126
+ return {
127
+ "Physics Chapters": physics_tracker.print_averages(),
128
+ "Chemistry Chapters": chemistry_tracker.print_averages(),
129
+ "Maths Chapters": maths_tracker.print_averages()
130
+ }
 
131
 
132
  except GoogleAuthError as e:
133
+ return {"error": f"Authentication error: {e}"}
134
  except gspread.exceptions.SpreadsheetNotFound:
135
+ return {"error": "Spreadsheet not found. Please check the URL."}
136
  except Exception as e:
137
+ return {"error": f"An error occurred: {e}"}
138
 
139
+ if __name__ == "__main__":
140
+ uvicorn.run(app, host="0.0.0.0", port=8000)