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