rairo commited on
Commit
25996b1
·
verified ·
1 Parent(s): 3bf5c0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import base64
4
+ import json
5
+ from scrapegraphai.graphs import SmartScraperGraph
6
+ import google.generativeai as genai
7
+ # Used to securely store your API key
8
+ from google.colab import userdata
9
+ import nest_asyncio
10
+ import os
11
+
12
+ nest_asyncio.apply()
13
+
14
+ GOOGLE_API_KEY = os.environ['Gemini']
15
+
16
+ graph_config = {
17
+ "llm": {
18
+ "api_key": GOOGLE_API_KEY,
19
+ "model": "google_genai/gemini-pro",
20
+ },
21
+ }
22
+
23
+ def get_data(url):
24
+ """
25
+ Fetches data from the given URL using scrapegraphai.
26
+
27
+ Args:
28
+ url: The URL to scrape.
29
+
30
+ Returns:
31
+ A dictionary containing the extracted data in the following format:
32
+ {'grants': [{'grant_name': ..., 'funding_organisation': ...,
33
+ 'due_date': ..., 'eligible_countries': ...,
34
+ 'eligibility_conditions': ...}, ...]}
35
+ """
36
+
37
+ smart_scraper_graph = SmartScraperGraph(
38
+ prompt="List me all grants or funds, the organisations funding them, the due date, eligible countries and eligibility conditions for applicants.",
39
+ source=url,
40
+ config=graph_config
41
+ )
42
+
43
+ result = smart_scraper_graph.run()
44
+ return result
45
+
46
+ def convert_to_csv(data):
47
+ df = pd.DataFrame(data['grants'])
48
+ return df.to_csv(index=False).encode('utf-8')
49
+
50
+ def convert_to_excel(data):
51
+ df = pd.DataFrame(data['grants'])
52
+ buffer = io.BytesIO()
53
+ with pd.ExcelWriter(buffer, engine='xlsxwriter') as writer:
54
+ df.to_excel(writer, sheet_name='Grants', index=False)
55
+ return buffer.getvalue()
56
+
57
+ st.title("Quantilytix Grant Scraper")
58
+
59
+ url = st.text_input("Enter URL")
60
+
61
+ if st.button("Get grants"):
62
+ if url:
63
+ try:
64
+ result = get_data(url)
65
+ st.success("Data scraped successfully!")
66
+
67
+ selected_format = st.selectbox("Select Download Format", ("CSV", "Excel"))
68
+
69
+ if selected_format == "CSV":
70
+ csv_data = convert_to_csv(result)
71
+ b64 = base64.b64encode(csv_data).decode()
72
+ download_link = f"<a href='data:application/vnd.ms-excel;base64,{b64}' download='grants.csv'>Download CSV</a>"
73
+ st.markdown(download_link, unsafe_allow_html=True)
74
+ elif selected_format == "Excel":
75
+ excel_data = convert_to_excel(result)
76
+ b64 = base64.b64encode(excel_data).decode()
77
+ download_link = f"<a href='data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}' download='grants.xlsx'>Download Excel</a>"
78
+ st.markdown(download_link, unsafe_allow_html=True)
79
+
80
+ st.dataframe(result['grants'])
81
+ except Exception as e:
82
+ st.error(f"Error scraping data: {e}")
83
+ else:
84
+ st.warning("Please enter a URL.")