|
import os |
|
import streamlit as st |
|
import requests |
|
import re |
|
|
|
|
|
session = requests.Session() |
|
|
|
|
|
def chat_with_ai(message, user_input): |
|
api_url = os.getenv("CHAT_API_URL") |
|
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') |
|
else: |
|
st.error("Failed to get a response from the AI API. π") |
|
except requests.RequestException as e: |
|
st.error(f"Error: {e}") |
|
|
|
|
|
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}") |
|
|
|
|
|
def sanitize_text(text): |
|
|
|
sanitized_text = re.sub(r'[^\x00-\x7F]+', '', text) |
|
return sanitized_text |
|
|
|
|
|
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...'): |
|
|
|
search_results = web_search(query) |
|
|
|
|
|
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 = "Powered by Webscout's free news API." |
|
|
|
|
|
with st.spinner('π Generating report...'): |
|
report = chat_with_ai(prompt, query) |
|
|
|
|
|
sanitized_report = sanitize_text(report + "\n\n" + outro) |
|
|
|
|
|
output_box = st.empty() |
|
|
|
|
|
output_box.text_area("Generated Report", value=sanitized_report, height=400) |
|
|
|
|
|
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() |