TechScout / app.py
Abhaykoul's picture
Update app.py
97a6b18 verified
raw
history blame
4.01 kB
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()