Serg4451D commited on
Commit
7ea8dbb
·
verified ·
1 Parent(s): 333f16b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -56
app.py CHANGED
@@ -1,6 +1,4 @@
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,65 +8,23 @@ st.title("Chat with GPT-4 Free")
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,10 +43,10 @@ if prompt := st.chat_input("Type your message here..."):
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})
 
1
  import streamlit as st
 
 
2
  import g4f
3
 
4
  # Set the title of the app
 
8
  if "messages" not in st.session_state:
9
  st.session_state.messages = []
10
 
11
+ # Model selection
12
  model_options = {
13
+ "GPT-4 Optimized": g4f.models.gpt_4o,
14
  "GPT-4": g4f.models.gpt_4,
15
+ "GPT-4 Turbo": g4f.models.gpt_4_turbo,
16
+ "GPT-4 Optimized Mini": g4f.models.gpt_4o_mini
17
  }
18
 
19
+ selected_model = st.selectbox("Choose a model:", list(model_options.keys()))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Function to generate responses from g4f
22
+ def generate_response(prompt):
 
 
 
 
 
 
23
  response = g4f.ChatCompletion.create(
24
+ model=model_options[selected_model],
25
  messages=[{"role": "user", "content": prompt}],
26
  stream=True # Enable streaming for real-time response
27
  )
 
28
  return response
29
 
30
  # Display chat messages from history
 
43
 
44
  # Generate and display AI response
45
  with st.chat_message("assistant"):
46
+ response = generate_response(prompt)
47
+ for msg in response:
48
+ if isinstance(msg, str): # Check if the message is a string
49
+ st.markdown(msg)
50
 
51
  # Add assistant's response to chat history
52
+ st.session_state.messages.append({"role": "assistant", "content": msg})