|
import os |
|
import streamlit as st |
|
import requests |
|
|
|
|
|
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 = os.getenv("SEARCH_API_URL") |
|
payload = {"keywords": keywords} |
|
response = requests.post(url, json=payload) |
|
|
|
if response.status_code == 200: |
|
return response.json() |
|
else: |
|
st.error(f"Error: {response.status_code}") |
|
|
|
|
|
def main(): |
|
st.set_page_config(page_title='TechScout Research Assistant', page_icon=":mag:") |
|
st.title("π TechScout Research Assistant") |
|
st.sidebar.header("π οΈ Settings") |
|
query = st.sidebar.text_input("π Enter your research query: ") |
|
generate_report = st.sidebar.button("π Generate Report") |
|
|
|
output = st.empty() |
|
|
|
if generate_report: |
|
if query: |
|
with st.spinner('π Searching...'): |
|
|
|
search_results = web_search(query) |
|
|
|
|
|
prompt = f"Generate a detailed research report based on the following information: {search_results}. The user's query was: '{query}'. Please include an overview, key findings, and any relevant details in the report. If the search results are insufficient, answer the user's question using the information available." |
|
with st.spinner('π Generating report...'): |
|
report = chat_with_ai(prompt, query) |
|
|
|
|
|
if isinstance(report, dict) and 'error' in report: |
|
st.error(report['error']) |
|
else: |
|
output.text_area("Generated Report", report, height=200, max_chars=None, key=None) |
|
|
|
else: |
|
st.sidebar.error("β Please enter a research query.") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|