Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
from transformers import pipeline
|
5 |
+
import tensorflow
|
6 |
+
|
7 |
+
# Function to search CrossRef using the user's query
|
8 |
+
def search_crossref(query, rows=10):
|
9 |
+
url = "https://api.crossref.org/works"
|
10 |
+
|
11 |
+
params = {
|
12 |
+
"query": query,
|
13 |
+
"rows": rows,
|
14 |
+
"filter": "type:journal-article"
|
15 |
+
}
|
16 |
+
|
17 |
+
try:
|
18 |
+
response = requests.get(url, params=params)
|
19 |
+
response.raise_for_status()
|
20 |
+
return response.json()
|
21 |
+
except requests.exceptions.HTTPError as e:
|
22 |
+
st.error(f"HTTP error occurred: {e}")
|
23 |
+
return None
|
24 |
+
except Exception as e:
|
25 |
+
st.error(f"An error occurred: {e}")
|
26 |
+
return None
|
27 |
+
|
28 |
+
# Function to display the results in a table format
|
29 |
+
def display_results(data):
|
30 |
+
if data:
|
31 |
+
items = data.get('message', {}).get('items', [])
|
32 |
+
if not items:
|
33 |
+
st.warning("No results found for the query.")
|
34 |
+
return
|
35 |
+
|
36 |
+
paper_list = []
|
37 |
+
for item in items:
|
38 |
+
paper = {
|
39 |
+
"Title": item.get('title', [''])[0],
|
40 |
+
"Author(s)": ', '.join([author['family'] for author in item.get('author', [])]),
|
41 |
+
"Journal": item.get('container-title', [''])[0],
|
42 |
+
"DOI": item.get('DOI', ''),
|
43 |
+
"Link": item.get('URL', ''),
|
44 |
+
"Published": item.get('issued', {}).get('date-parts', [[None]])[0][0] if 'issued' in item else "N/A"
|
45 |
+
}
|
46 |
+
paper_list.append(paper)
|
47 |
+
|
48 |
+
df = pd.DataFrame(paper_list)
|
49 |
+
st.write(df)
|
50 |
+
else:
|
51 |
+
st.warning("No data to display.")
|
52 |
+
|
53 |
+
# Function to summarize text using the specified model
|
54 |
+
def summarize_text(text):
|
55 |
+
try:
|
56 |
+
# Initialize the summarization model
|
57 |
+
summarizer = pipeline("text2text-generation", model="spacemanidol/flan-t5-large-website-summarizer")
|
58 |
+
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
|
59 |
+
return summary[0]['generated_text']
|
60 |
+
except Exception as e:
|
61 |
+
st.error(f"An error occurred during summarization: {e}")
|
62 |
+
return "Summary could not be generated."
|
63 |
+
|
64 |
+
# Function to generate text (if you want to keep this)
|
65 |
+
def generate_text(text):
|
66 |
+
try:
|
67 |
+
# Initialize the text generation model
|
68 |
+
text_generator = pipeline("text2text-generation", model="JorgeSarry/est5-summarize")
|
69 |
+
generated_text = text_generator(text, max_length=150, min_length=50, do_sample=False)
|
70 |
+
return generated_text[0]['generated_text']
|
71 |
+
except Exception as e:
|
72 |
+
st.error(f"An error occurred during text generation: {e}")
|
73 |
+
return "Generated text could not be created."
|
74 |
+
|
75 |
+
|
76 |
+
# Main function
|
77 |
+
if __name__ == "__main__":
|
78 |
+
# Start Streamlit App
|
79 |
+
st.title("Research Paper Finder and Text Summarizer")
|
80 |
+
|
81 |
+
# Section for Research Paper Finder
|
82 |
+
st.subheader("Find Research Papers")
|
83 |
+
query = st.text_input("Enter your research topic or keywords", value="machine learning optimization")
|
84 |
+
num_papers = st.slider("Select number of papers to retrieve", min_value=5, max_value=50, value=10)
|
85 |
+
|
86 |
+
if st.button("Search"):
|
87 |
+
if query:
|
88 |
+
with st.spinner('Searching for papers...'):
|
89 |
+
response_data = search_crossref(query, rows=num_papers)
|
90 |
+
display_results(response_data)
|
91 |
+
else:
|
92 |
+
st.warning("Please enter a search query.")
|
93 |
+
|
94 |
+
# Section for Text Summarizer
|
95 |
+
st.subheader("Summarize Text")
|
96 |
+
user_text = st.text_area("Enter text to summarize", height=200)
|
97 |
+
|
98 |
+
if st.button("Summarize"):
|
99 |
+
if user_text:
|
100 |
+
with st.spinner('Summarizing text...'):
|
101 |
+
summary = summarize_text(user_text)
|
102 |
+
st.success("Summary:")
|
103 |
+
st.write(summary)
|
104 |
+
else:
|
105 |
+
st.warning("Please enter text to summarize.")
|
106 |
+
|
107 |
+
if st.button("Generate Text"):
|
108 |
+
if user_text:
|
109 |
+
with st.spinner('Generating text...'):
|
110 |
+
generated = generate_text(user_text)
|
111 |
+
st.success("Generated Text:")
|
112 |
+
st.write(generated)
|
113 |
+
else:
|
114 |
+
st.warning("Please enter text to generate from.")
|