File size: 2,315 Bytes
d5f4461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c6d734
d5f4461
 
 
 
 
9c6d734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5f4461
 
 
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
import streamlit as st
import requests

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

# Function to interact with the AI
def chat_with_ai(message):
    api_url = "https://free-ai-api.devastation-war.repl.co/chat"
    payload = {"message": message}

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

# Function to perform web search
def web_search(query):
    url = "https://test.devastation-war.repl.co/search"
    payload = {"query": query}
    response = requests.post(url, json=payload)

    if response.status_code == 200:
        return response.json().get('results')  # Assuming 'results' contain relevant info
    else:
        return {"error": f"Error: {response.status_code}"}

# Main function
def main():
    st.title("AI Research Assistant")
    st.write("This tool helps you generate a research report based on the text obtained from a web search.")
    
    query = st.text_area("Enter your search query:", value="", height=200, max_chars=None, key=None)
    
    if st.button("Generate Report"):
        if query:
            # Perform web search
            search_results = web_search(query)
            
            # Extract relevant information and convert it into a message
            message = ""  # Create an empty string to store the message
            for result in search_results:
                # Assuming each result is a string, you might adjust this part accordingly
                message += result + ". "  # Concatenate results with a period
            
            # Combine prompt and message for AI conversation
            full_message = f"Generate a research report based on the following text: {message}"
            
            # Pass the combined message to the AI for generating a report
            report = chat_with_ai(full_message)
            
            # Display the report
            st.write(report)
        else:
            st.write("Please enter a search query.")

if __name__ == "__main__":
    main()