File size: 4,010 Bytes
a3c8aff
d5f4461
 
3e45166
d5f4461
 
 
 
 
4e15bfb
0875530
4e15bfb
d5f4461
 
 
 
75b9eb6
d5f4461
aebe303
d5f4461
aebe303
d5f4461
 
37ae59d
e54d172
 
 
d5f4461
 
b4fac10
d5f4461
aebe303
d5f4461
3e45166
 
 
 
 
 
d5f4461
 
45fe636
 
64375ed
 
45fe636
64375ed
 
71cce77
64375ed
 
 
71cce77
45fe636
75b9eb6
 
 
 
45fe636
71cce77
64375ed
4e15bfb
71cce77
3e45166
 
 
97a6b18
 
 
 
 
 
3e45166
 
b36f425
a65a694
b36f425
a65a694
b36f425
 
71cce77
64375ed
d5f4461
 
b36f425
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
import os
import streamlit as st
import requests
import re # Import the re module for regular expressions

# Create a session for reusing connections
session = requests.Session()

# Function to interact with the AI
def chat_with_ai(message, user_input):
    api_url = os.getenv("CHAT_API_URL") # Vortex 3b models API
    payload = {"message": message, "user_input": user_input}

    try:
        with session.post(api_url, json=payload) as response:
            if response.status_code == 200:
                return response.json().get('response') # Access 'response' key
            else:
                st.error("Failed to get a response from the AI API. 😞")
    except requests.RequestException as e:
        st.error(f"Error: {e}")

# Function to perform web search
def web_search(keywords):
    url = "https://abhaykoul-webscout1.hf.space/news"
    params = {"keywords": keywords}
    response = requests.get(url, params=params)

    if response.status_code == 200:
        return response.json()
    else:
        st.error(f"Error: {response.status_code}")

# Function to sanitize the output text
def sanitize_text(text):
    # Remove or replace unwanted characters
    sanitized_text = re.sub(r'[^\x00-\x7F]+', '', text)
    return sanitized_text

# Main function
def main():
    st.set_page_config(page_title='TechScout News assistant', page_icon=":mag:")
    st.title("πŸ” TechScout News Assistant")
    st.sidebar.header("πŸ› οΈ Settings")
    query = st.sidebar.text_input("πŸ”Ž Enter your research query: ")
    generate_report = st.sidebar.button("πŸ“ Generate News report")

    if generate_report:
        if query:
            with st.spinner('πŸ”„ Searching...'):
                # Perform web search
                search_results = web_search(query)
            
            # Prepare prompt based on provided guidelines
            prompt = f"User's query: {query}, Search Results: {search_results}. As TechScout, focus on AI innovations, share latest technological news, and maintain a pleasant atmosphere. Adopt a professional yet amiable demeanor, appealing to individuals with varying expertise levels. Inspire enthusiasm and investigation in science and technology, presenting facts and insights tailored to the user's interests. Use easy wordings for broad understanding. Don't include news sources or datetimes. Concentrate on breakthroughs and improvements in AI development. Integrate witty remarks or comical references to engage the user. Produce cogent, error-free replies demonstrating language skills and a comprehensive grasp of the subject. Enhance proficiency by learning from previous encounters and understanding user inclinations and technology trends. Explain each news item. Stay ahead with exciting AI developments! πŸŒŸβž‘οΈπŸ’‘"
            
            # Outro for the AI to conclude the report
            outro = "Powered by Webscout's free news API."
            
            # Pass the search results to the AI for generating a report
            with st.spinner('πŸ”„ Generating report...'):
                report = chat_with_ai(prompt, query)
            
            # Sanitize the report text
            sanitized_report = sanitize_text(report + "\n\n" + outro)
            
            # Create a text area for the output
            output_box = st.empty()
            
            # Display the sanitized report in the output box
            output_box.text_area("Generated Report", value=sanitized_report, height=400)
            
            # Add a copy button to copy the output
            if st.button('Copy Report'):
                try:
                    st.success("Report copied to clipboard!")
                    st.write(sanitized_report)
                    st.experimental_set_state({"report_copied": True})
                except Exception as e:
                    st.error(f"Error copying report: {e}")
        else:
            st.sidebar.error("❗ Please enter a research query.")

if __name__ == "__main__":
    main()