File size: 4,203 Bytes
4946a6b
 
 
d42497f
4946a6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21fc602
 
4946a6b
 
 
 
 
 
 
 
 
21fc602
 
4946a6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import pandas as pd
import transformers
from transformers import pipeline
import tensorflow

# Function to search CrossRef using the user's query
def search_crossref(query, rows=10):
    url = "https://api.crossref.org/works"
    
    params = {
        "query": query,
        "rows": rows,
        "filter": "type:journal-article"
    }
    
    try:
        response = requests.get(url, params=params)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.HTTPError as e:
        st.error(f"HTTP error occurred: {e}")
        return None
    except Exception as e:
        st.error(f"An error occurred: {e}")
        return None

# Function to display the results in a table format
def display_results(data):
    if data:
        items = data.get('message', {}).get('items', [])
        if not items:
            st.warning("No results found for the query.")
            return
        
        paper_list = []
        for item in items:
            paper = {
                "Title": item.get('title', [''])[0],
                "Author(s)": ', '.join([author['family'] for author in item.get('author', [])]),
                "Journal": item.get('container-title', [''])[0],
                "DOI": item.get('DOI', ''),
                "Link": item.get('URL', ''),
                "Published": item.get('issued', {}).get('date-parts', [[None]])[0][0] if 'issued' in item else "N/A"
            }
            paper_list.append(paper)
        
        df = pd.DataFrame(paper_list)
        st.write(df)
    else:
        st.warning("No data to display.")

# Function to summarize text using the specified model
def summarize_text(text):
    try:
        # Initialize the summarization model with PyTorch
        summarizer = pipeline("text2text-generation", model="spacemanidol/flan-t5-large-website-summarizer", framework="pt")
        summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
        return summary[0]['generated_text']
    except Exception as e:
        st.error(f"An error occurred during summarization: {e}")
        return "Summary could not be generated."

# Function to generate text (if you want to keep this)
def generate_text(text):
    try:
        # Initialize the text generation model with PyTorch
        text_generator = pipeline("text2text-generation", model="JorgeSarry/est5-summarize", framework="pt")
        generated_text = text_generator(text, max_length=150, min_length=50, do_sample=False)
        return generated_text[0]['generated_text']
    except Exception as e:
        st.error(f"An error occurred during text generation: {e}")
        return "Generated text could not be created."


# Main function
if __name__ == "__main__":
    # Start Streamlit App
    st.title("Research Paper Finder and Text Summarizer")

    # Section for Research Paper Finder
    st.subheader("Find Research Papers")
    query = st.text_input("Enter your research topic or keywords", value="machine learning optimization")
    num_papers = st.slider("Select number of papers to retrieve", min_value=5, max_value=50, value=10)

    if st.button("Search"):
        if query:
            with st.spinner('Searching for papers...'):
                response_data = search_crossref(query, rows=num_papers)
                display_results(response_data)
        else:
            st.warning("Please enter a search query.")

    # Section for Text Summarizer
    st.subheader("Summarize Text")
    user_text = st.text_area("Enter text to summarize", height=200)

    if st.button("Summarize"):
        if user_text:
            with st.spinner('Summarizing text...'):
                summary = summarize_text(user_text)
                st.success("Summary:")
                st.write(summary)
        else:
            st.warning("Please enter text to summarize.")

    if st.button("Generate Text"):
        if user_text:
            with st.spinner('Generating text...'):
                generated = generate_text(user_text)
                st.success("Generated Text:")
                st.write(generated)
        else:
            st.warning("Please enter text to generate from.")