Mr-Thop commited on
Commit
8fff8c9
·
1 Parent(s): fad0066

Add: Flask File , req.txt , Testing files

Browse files
Files changed (4) hide show
  1. api_try.ipynb +0 -0
  2. req.txt +9 -0
  3. rm.py +202 -0
  4. test_agent.py +16 -0
api_try.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
req.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ flask==2.3.3
2
+ flask[async]
3
+ flask-cors==4.0.0
4
+ google-generativeai==0.3.2
5
+ requests==2.31.0
6
+ google-search-results
7
+ firecrawl-py
8
+ google-genai
9
+ gunicorn
rm.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from socket import timeout
2
+ from serpapi import GoogleSearch
3
+ import os
4
+ from firecrawl import FirecrawlApp
5
+ from flask import Flask, request, jsonify
6
+ from flask_cors import CORS
7
+ from google import genai
8
+ import json
9
+
10
+
11
+
12
+
13
+ f_app = FirecrawlApp(api_key=os.getenv("FIRECRAWL_API_KEY"))
14
+ app = Flask(__name__)
15
+ CORS(app)
16
+
17
+ client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
18
+
19
+
20
+ SYSTEM_PROMPT = os.getenv("SYSTEM_PROMPT")
21
+
22
+ def get_google_scholar_results(key_params: dict):
23
+ key_params['api_key'] = os.getenv("SERPAPI_API_KEY")
24
+ key_params['engine'] = "google_scholar"
25
+ key_params['hl'] = "en"
26
+ search = GoogleSearch(key_params)
27
+ results = search.get_dict()
28
+ if "profiles" in results:
29
+ return results["profiles"]["authors"],results["organic_results"]
30
+ else:
31
+ return None,results["organic_results"]
32
+
33
+ def get_results(query: str):
34
+ '''
35
+ This function is used to get the results from the Google Scholar API.
36
+ It takes a query as input and returns a list of dictionaries, each containing the information about a paper/author.
37
+ The keys of the dictionaries are the fields of the paper.
38
+
39
+ Keys of the dictionary are:
40
+ dict_keys(['position', 'title', 'result_id', 'link', 'snippet', 'publication_info', 'resources', 'inline_links'])
41
+ '''
42
+ params = {
43
+ "q": query,
44
+ }
45
+
46
+ answer = []
47
+
48
+ profiles,result = get_google_scholar_results(params)
49
+ keys = result[0].keys()
50
+ for i in range(len(result)):
51
+ output = {}
52
+ if "title" in result[i]:
53
+ output["title"] = result[i]["title"]
54
+ if "result_id" in result[i]:
55
+ output["result_id"] = result[i]["result_id"]
56
+ if "link" in result[i]:
57
+ output["link"] = result[i]["link"]
58
+ print(f"\n {output['link']}")
59
+ if "https://www.annualreviews" in result[i]["link"]:
60
+ output["abstract"] = get_abstract(result[i]["link"])
61
+ if "snippet" in result[i]:
62
+ output["snippet"] = result[i]["snippet"]
63
+ if "publication_info" in result[i]:
64
+ output["publication_info"] = result[i]["publication_info"]
65
+ if "resources" in result[i]:
66
+ output["resources"] = result[i]["resources"]
67
+
68
+ answer.append(output)
69
+
70
+
71
+ return profiles,answer,keys
72
+
73
+ def get_abstract(url: str):
74
+ scrape_result = f_app.scrape_url(url, formats=['markdown', 'html'])
75
+ if "Abstract" in scrape_result.html:
76
+ offset = scrape_result.html.find("Abstract")
77
+ start = scrape_result.html[offset:].find("<p>")
78
+ end = scrape_result.html[offset+start:].find("</p>")
79
+ print(offset,start,end,sep="\t")
80
+ print(f"\n {scrape_result.html[offset+start:offset+start+end]}")
81
+ return scrape_result.html[offset+start:offset+start+end]
82
+ else:
83
+ return "Abstract not found"
84
+
85
+ def scrape_web(url:str):
86
+ '''
87
+ This function is used inorder to scrape any websitye based on its url
88
+ Returns the html code of the webpage
89
+ '''
90
+ scrape_result = f_app.scrape_url(url, formats=['markdown', 'html'])
91
+ return scrape_result.html
92
+
93
+ async def get_response(chat_client,user):
94
+ response = chat_client.send_message(user)
95
+ return response.candidates[0].content.parts[0].text
96
+
97
+ def convert_to_json(text):
98
+ start = text.find("{")
99
+ end = text[::-1].find("}")
100
+ json_text = text[start : -end]
101
+ try:
102
+ return json.loads(json_text)
103
+ except Exception as e:
104
+ return "Json Parse Error due to " + str(e)
105
+
106
+ def get_observation(function,inp):
107
+ functions = ["get_results","scrape_web"]
108
+ if function == functions[0]:
109
+ profiles,answer,keys = get_results(inp)
110
+ out_dict = {
111
+ "state" : "OBSERVATION",
112
+ "observation" : {
113
+ "profiles" : profiles,
114
+ "answer" : answer,
115
+ "keys" : keys
116
+ }
117
+ }
118
+ elif function == functions[1]:
119
+ html_text = scrape_web(inp)
120
+ out_dict = {
121
+ "state" : "OBSERVATION",
122
+ "observation" : {
123
+ "html_text" : html_text
124
+ }
125
+ }
126
+ else:
127
+ out_dict = {
128
+ "state" : "OBSERVATION",
129
+ "observation" : {
130
+ "message":"Function Not found, Please Retry"
131
+ }
132
+ }
133
+ return out_dict
134
+
135
+ async def get_output(chat_client,inp):
136
+ response = await get_response(chat_client,str(inp))
137
+ output = convert_to_json(response)
138
+ while output["state"] != "OUTPUT":
139
+ if output["state"] == "PLAN":
140
+ response = await get_response(chat_client,str(output))
141
+ output = convert_to_json(response)
142
+ print(output)
143
+ elif output["state"] == "CALL":
144
+ function = output["function_name"]
145
+ for i in output["params"].keys():
146
+ inp = output["params"][i]
147
+ print(inp)
148
+ obs = get_observation(function,inp)
149
+ response = await get_response(chat_client,str(obs))
150
+ output = convert_to_json(response)
151
+ print(output)
152
+ elif output["state"] == "OBSERVATION":
153
+ response = await get_response(chat_client,str(output))
154
+ output = convert_to_json(response)
155
+ print(output)
156
+ else:
157
+ response = await get_response(chat_client,str(output))
158
+ output = convert_to_json(response)
159
+ print(output)
160
+ return output
161
+
162
+
163
+
164
+
165
+ async def chat(query: str):
166
+ chat_client = client.chats.create(
167
+ model="gemini-2.5-flash"
168
+ )
169
+ response = await get_response(chat_client,SYSTEM_PROMPT)
170
+ inp = {
171
+ "state" : "START",
172
+ "user" : query
173
+ }
174
+
175
+ output = await get_output(chat_client,inp)
176
+ return output["output"]
177
+
178
+ @app.route("/",methods=["GET"])
179
+ def default():
180
+ return jsonify({"message": "Backend Working Successfully"})
181
+
182
+ @app.route("/chat",methods=["GET"])
183
+ async def get_chat_results():
184
+ query = request.json.get("query")
185
+ output = await chat(query)
186
+ return jsonify({"output":output})
187
+
188
+
189
+ if __name__ == "__main__":
190
+ app.run(debug=True)
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
test_agent.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from google import genai
2
+ import os
3
+
4
+ client = genai.Client(api_key=os.getenv("API_KEY"))
5
+
6
+ with open("prompt.txt", "r") as f:
7
+ prompt = f.read()
8
+
9
+ chat_client = client.chats.create(
10
+ model="gemini-2.5-flash"
11
+ )
12
+
13
+ response = chat_client.send_message(prompt)
14
+
15
+
16
+ print(response.text)