Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import g4f
|
3 |
|
4 |
# Set the title of the app
|
@@ -8,13 +10,65 @@ st.title("Chat with GPT-4 Free")
|
|
8 |
if "messages" not in st.session_state:
|
9 |
st.session_state.messages = []
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Function to generate responses from g4f
|
12 |
-
def generate_response(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
response = g4f.ChatCompletion.create(
|
14 |
-
model=
|
15 |
messages=[{"role": "user", "content": prompt}],
|
16 |
stream=True # Enable streaming for real-time response
|
17 |
)
|
|
|
18 |
return response
|
19 |
|
20 |
# Display chat messages from history
|
@@ -33,10 +87,10 @@ if prompt := st.chat_input("Type your message here..."):
|
|
33 |
|
34 |
# Generate and display AI response
|
35 |
with st.chat_message("assistant"):
|
36 |
-
response = generate_response(prompt)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
# Add assistant's response to chat history
|
42 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
import g4f
|
5 |
|
6 |
# Set the title of the app
|
|
|
10 |
if "messages" not in st.session_state:
|
11 |
st.session_state.messages = []
|
12 |
|
13 |
+
# Dropdown for model selection
|
14 |
+
model_options = {
|
15 |
+
"GPT-3.5 Turbo": g4f.models.gpt_3_5_turbo,
|
16 |
+
"GPT-4": g4f.models.gpt_4,
|
17 |
+
"GPT-4o": g4f.models.gpt_4o,
|
18 |
+
}
|
19 |
+
|
20 |
+
selected_model = st.sidebar.selectbox("Select Model", list(model_options.keys()))
|
21 |
+
|
22 |
+
# Toggle for internet search functionality
|
23 |
+
search_enabled = st.sidebar.checkbox("Enable Internet Search")
|
24 |
+
|
25 |
+
# Function to perform a Google search
|
26 |
+
def google_search(query):
|
27 |
+
headers = {
|
28 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
|
29 |
+
}
|
30 |
+
|
31 |
+
# Construct the search URL
|
32 |
+
url = f"https://www.google.com/search?q={query}"
|
33 |
+
|
34 |
+
# Send the request to Google
|
35 |
+
response = requests.get(url, headers=headers)
|
36 |
+
|
37 |
+
# Parse the HTML content
|
38 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
39 |
+
|
40 |
+
# Find all search result containers
|
41 |
+
results = soup.find_all('div', class_='g')
|
42 |
+
|
43 |
+
# Extract titles and summaries
|
44 |
+
search_results = []
|
45 |
+
|
46 |
+
for result in results:
|
47 |
+
title = result.find('h3')
|
48 |
+
summary = result.find('span', class_='aCOpRe')
|
49 |
+
|
50 |
+
if title and summary:
|
51 |
+
search_results.append({
|
52 |
+
'title': title.text,
|
53 |
+
'summary': summary.text
|
54 |
+
})
|
55 |
+
|
56 |
+
return search_results
|
57 |
+
|
58 |
# Function to generate responses from g4f
|
59 |
+
def generate_response(prompt, model, search=False):
|
60 |
+
if search:
|
61 |
+
# Perform a web search and return results
|
62 |
+
search_results = google_search(prompt)
|
63 |
+
formatted_results = "\n".join([f"{result['title']}: {result['summary']}" for result in search_results])
|
64 |
+
return f"Search Results:\n{formatted_results}" if formatted_results else "No results found."
|
65 |
+
|
66 |
response = g4f.ChatCompletion.create(
|
67 |
+
model=model,
|
68 |
messages=[{"role": "user", "content": prompt}],
|
69 |
stream=True # Enable streaming for real-time response
|
70 |
)
|
71 |
+
|
72 |
return response
|
73 |
|
74 |
# Display chat messages from history
|
|
|
87 |
|
88 |
# Generate and display AI response
|
89 |
with st.chat_message("assistant"):
|
90 |
+
response = generate_response(prompt, model_options[selected_model], search=search_enabled)
|
91 |
+
|
92 |
+
if isinstance(response, str): # Check if the response is a string
|
93 |
+
st.markdown(response)
|
94 |
|
95 |
# Add assistant's response to chat history
|
96 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|