-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.txt
More file actions
211 lines (158 loc) · 4.95 KB
/
code.txt
File metadata and controls
211 lines (158 loc) · 4.95 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
git add .
git commit -m "Fix CORS issue for frontend"
git push origin main
# from fastapi import FastAPI, Request
# from fastapi.middleware.cors import CORSMiddleware
# from pydantic import BaseModel
# import os
# from dotenv import load_dotenv
# import google.generativeai as genai
# # Load .env
# load_dotenv()
# # Configure Gemini
# genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# model = genai.GenerativeModel('gemini-2.5-flash')
# # FastAPI app
# app = FastAPI()
# # CORS setup for frontend communication
# app.add_middleware(
# CORSMiddleware,
# allow_origins=["*"], # Replace with frontend URL in production
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
# # Input model
# class CodeInput(BaseModel):
# code: str
# action: str # "run", "tc", "sc", or "optimal"
# # Utility functions
# def compiler(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Find the output of the given code.
# 2. If the code contains any **errors**, identify them along with the **line number** where they occur.
# 3. Don't provide explanation. Just return:
# - Output: (if there is no error)
# - Error: (if any)
# Code:
# {code}
# """
# return model.generate_content(prompt).text
# def time_complexity(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Determine the **time complexity** of the code.
# 2. Provide **optimal time complexity** if possible.
# 3. No explanation.
# Code:
# {code}
# """
# return model.generate_content(prompt).text
# def space_complexity(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Determine the **space complexity** of the code.
# 2. Provide **optimal space complexity** if possible.
# 3. No explanation.
# Code:
# {code}
# """
# return model.generate_content(prompt).text
# def optimal(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Provide an **optimal version** of the code.
# 2. Mention **time complexity**.
# 3. No explanation.
# Code:
# {code}
# """
# return model.generate_content(prompt).text
# # Main endpoint
# @app.post("/api/execute")
# async def execute_code(data: CodeInput):
# code = data.code
# action = data.action.lower()
# if action == "run":
# result = compiler(code)
# elif action == "tc":
# result = time_complexity(code)
# elif action == "sc":
# result = space_complexity(code)
# elif action == "optimal":
# result = optimal(code)
# else:
# result = "❌ Invalid action."
# return {"result": result}
# import os
# from dotenv import load_dotenv
# import google.generativeai as genai
# load_dotenv()
# genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# model = genai.GenerativeModel('gemini-2.5-flash')
# def readcode(filepath):
# """Read code from a file."""
# try:
# with open(filepath, "r") as f:
# return f.read()
# except FileNotFoundError:
# print("❌ Error: code file not found.")
# exit(1)
# def compiler(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Find the output of the given code.
# 2. If the code contains any **errors**, identify them along with the **line number** where they occur.
# 3. Dont provide any Explaination just provide the Output
# Code:
# {code}
# Your explanation should be structured as:
# - Output: (if there is no error)
# - Error: (if the code has a mistake)
# """
# try:
# response = model.generate_content(prompt)
# return response.text
# except Exception as e:
# print(f"An error occurred: {e}")
# exit(1)
# def time_complexity(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Determine the **time complexity** of the given code.
# 2. If possible, also provide the **optimal time complexity**.
# 3. Dont provide Explaination just provide time complexity
# Code:
# {code}
# Your explanation should be structured as:
# - Time Complexity: (actual time complexity)
# - Optimal Time Complexity: (if any improvements are possible)
# """
# try:
# response = model.generate_content(prompt)
# return response.text
# except Exception as e:
# print(f"An error occurred: {e}")
# exit(1)
# def optimal(code):
# prompt = f"""
# You are a professional programming assistant.
# 1. Provide an **optimal version** of the given code. Improve its performance, readability, or efficiency if possible.
# 2. Identify the **time complexity** of the code.
# 3. Dont Explain the code just provide the Optimal version
# Code:
# {code}
# Your explanation should be structured as:
# - Optimal Code:
# """
# try:
# response = model.generate_content(prompt)
# return response.text
# except Exception as e:
# print(f"An error occurred: {e}")
# exit(1)
# if __name__ == '__main__':
# code=readcode("code.txt")
# r=optimal(code)
# print(r)