Skip to content

Commit

Permalink
Added the 'Model Selection' feature to the GUI Settings, you can now …
Browse files Browse the repository at this point in the history
…choose a model from the settings without the need to pick a specific provider. Bing's tones are now under the model's list. Decluttered code and files. Fixed Pytest.
  • Loading branch information
aledipa authored and git-malik committed Apr 21, 2024
1 parent cf5bb45 commit 738f9d8
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM python:3.12-slim
WORKDIR /app/
COPY . .

RUN apt update && apt install gcc -y
RUN apt update && apt install gcc chromium-browser -y
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ auth
Flask[async]
g4f
Werkzeug
aiohttp_socks
nodriver
Binary file modified src/.DS_Store
Binary file not shown.
59 changes: 30 additions & 29 deletions src/FreeGPT4_Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename

app = Flask(__name__)


app = Flask(__name__)

UPLOAD_FOLDER = 'data/'
# ALLOWED_EXTENSIONS = {'json'}

Expand All @@ -30,6 +31,7 @@

# Available providers
PROVIDERS = {
"Auto": "",
"AItianhu": g4f.Provider.AItianhu,
"Acytoo": g4f.Provider.Acytoo,
"Aichat": g4f.Provider.Aichat,
Expand All @@ -52,12 +54,8 @@
"Gemini": g4f.Provider.Gemini,
}

# Available Bing's tones
TONES = [
"Creative",
"Balanced",
"Precise"
]
GENERIC_MODELS = ["gpt-3.5-turbo", "gpt-4"]


print(
"""
Expand Down Expand Up @@ -140,13 +138,6 @@
type=str,
help="Add the keyword support",
)
parser.add_argument(
"--tone",
action='store',
required=False,
type=str,
help="Specify the model's tone if supported (Bing's default: Precise)",
)
parser.add_argument(
"--system-prompt",
action='store',
Expand All @@ -157,6 +148,13 @@

args, unknown = parser.parse_known_args()

# Get the absolute path of the script
script_path = os.path.abspath(__file__)
# Get the directory of the script
script_dir = os.path.dirname(script_path)
# Change the current working directory
os.chdir(script_dir)

# Loads the settings from the file
with open(SETTINGS_FILE, "r") as f:
data = json.load(f)
Expand Down Expand Up @@ -193,11 +191,6 @@
data["cookie_file"] = args.cookie_file
else:
args.cookie_file = data["cookie_file"]

if (args.tone != None):
data["tone"] = args.tone
else:
args.tone = data["tone"]

if (args.system_prompt != None):
data["system_prompt"] = args.system_prompt
Expand Down Expand Up @@ -264,10 +257,9 @@ def saveSettings(request, file):
data["file_input"] = bool(request.form["file_input"] == "true")
data["remove_sources"] = bool(request.form["remove_sources"] == "true")
data["port"] = request.form["port"]
# data["model"] = request.form["model"] #Considering to implement this
data["model"] = request.form["model"] #Considering to implement this
data["keyword"] = request.form["keyword"]
data["provider"] = request.form["provider"]
data["tone"] = request.form["tone"]
data["system_prompt"] = request.form["system_prompt"]
data["message_history"] = bool(request.form["message_history"] == "true")
file = request.files["cookie_file"]
Expand Down Expand Up @@ -301,7 +293,6 @@ def applySettings(file):
args.cookie_file = data["cookie_file"]
args.token = data["token"]
args.remove_sources = data["remove_sources"]
args.tone = data["tone"]
args.system_prompt = data["system_prompt"]
args.enable_history = data["message_history"]
f.close()
Expand All @@ -319,16 +310,12 @@ async def index() -> str:
# Starts the bot and gets the input
print("Initializing...")
question = None
tone = None

print("start")
if request.method == "GET":
question = request.args.get(args.keyword) #text
tone = request.args.get("tone") #tone
if (args.private_mode and request.args.get("token") != data["token"]):
return "<p id='response'>Invalid token</p>"
if (tone == None or tone not in TONES):
tone = args.tone
print("get")
else:
file = request.files["file"]
Expand All @@ -344,7 +331,6 @@ async def index() -> str:
# print(PROVIDERS[args.provider].params) # supported args
print("\nCookies: " + str(len(args.cookie_file)))
print("\nInput: " + question)
print("\nTone: " + tone)
if (len(args.cookie_file) != 0):
try:
cookies = json.load(open(args.cookie_file)) # Loads the cookies from the file
Expand All @@ -367,17 +353,21 @@ async def index() -> str:
message_history.append({"role": "system", "content": args.system_prompt})
message_history.append({"role": "user", "content": question})

if (args.provider == "Auto"):
args.provider = ""

# Set with provider
response = (
await g4f.ChatCompletion.create_async(
model=args.model,
provider=args.provider,
messages=message_history,
cookies=cookies,
auth=True,
tone=tone
)
)

print(response)

#Joins the response into a single string
resp_str = ""
for message in response:
Expand Down Expand Up @@ -422,6 +412,7 @@ async def settings():
if (auth()):
try:
providers=PROVIDERS
generic_models=GENERIC_MODELS
data = json.loads(open(SETTINGS_FILE).read())
return render_template("settings.html", **locals())
except Exception as error:
Expand All @@ -447,6 +438,16 @@ async def save():
else:
return render_template("login.html", **locals())

@app.route("/models", methods=["GET"])
async def get_models():
provider = request.args.get("provider")
if (provider == "Auto"):
return GENERIC_MODELS
try:
return PROVIDERS[provider].models
except:
return ["default"]

if __name__ == "__main__":
# Starts the server, change the port if needed
app.run("0.0.0.0", port=args.port, debug=False)
Binary file modified src/__pycache__/FreeGPT4_Server.cpython-311.pyc
Binary file not shown.
Binary file modified src/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
5 changes: 2 additions & 3 deletions src/data/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"remove_sources": true,
"port": "5500",
"model": "gpt-4",
"provider": "Bing",
"tone": "Precise",
"provider": "",
"keyword": "text",
"cookie_file": "data/cookies.json",
"system_prompt": ""
}
}
31 changes: 30 additions & 1 deletion src/static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,33 @@ function showElement(id_to_show) {
// Hides an element
function hideElement(id_to_hide) {
document.getElementById(id_to_hide).hidden = true;
}
}

// Gets available models for choosen A.I. Provider
$(document).ready(function(){
$("#provider").change(function(){
var inputValue = $(this).val();
$.ajax({
url: '/models',
data: {
'provider': inputValue
},
success: function(response) {
var select = document.getElementById("model");

// Remove existing models
while (select.firstChild) {
select.removeChild(select.firstChild);
}

// Add the new models
for (var i=0; i<response.length; i++) {
var option = document.createElement("option");
option.innerText = response[i];
option.value = response[i];
select.add(option);
}
}
});
});
});
37 changes: 14 additions & 23 deletions src/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ <h2 class="title blue text-2xl">Server settings</h2>
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800"><b>A.I. Provider:</b></td>
<td class="py-1 fond-bold inter darkblue text-lg">
<b>
<select name="provider" class="input outline-none py-1 px-2 rounded-lg inter w-24">
<select name="provider" id="provider" class="input outline-none py-1 px-2 rounded-lg inter w-24">
<option value="{{ data['provider'] }}"> {{ data['provider'] }} - Default </option>
{% for key, value in providers.items() %}
<option value="{{ key }}"> {{ key }} </option>
Expand All @@ -100,19 +100,24 @@ <h2 class="title blue text-2xl">Server settings</h2>
</b>
</td>
</tr>
<!-- Considering to implement this -->
<!-- <tr>
<tr>
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800"><b>A.I. Model:</b></td>
<td class="py-1 fond-bold inter darkblue text-lg">
<b>
<select name="model" class="input outline-none py-1 px-2 rounded-lg inter w-24">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<select name="model" id="model" class="input outline-none py-1 px-2 rounded-lg inter w-24">
{% if data['provider'] == "Auto" %}
{% for model in generic_models %}
<option value="{{ model }}"> {{ model }} </option>
{% endfor %}
{% else %}
{% for model in providers[data['provider']].models %}
<option value="{{ model }}"> {{ model }} </option>
{% endfor %}
{% endif %}
</select>
</b>
</td>
</tr> -->
</tr>
<tr>
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800"><b>Cookies:</b></td>
<td class="py-1 fond-bold inter darkblue text-lg">
Expand All @@ -134,18 +139,6 @@ <h2 class="title blue text-2xl">Server settings</h2>
</b>
</td>
</tr>
<tr>
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800"><b>Tone:</b></td>
<td class="py-1 fond-bold inter darkblue text-lg">
<b>
<select name="tone" class="input outline-none py-1 px-2 rounded-lg inter w-24">
<option value="Precise">Precise</option>
<option value="Creative">Creative</option>
<option value="Balanced">Balanced</option>
</select>
</b>
</td>
</tr>
<tr>
<td class="py-1 fond-bold inter darkblue text-lg border-b border-slate-800">
<b>System Prompt:</b>
Expand All @@ -166,14 +159,12 @@ <h2 class="title blue text-2xl">Server settings</h2>
<label for="save" id="save_label" class="button outline-none py-3 px-4 pl-5 rounded-lg darkblue_bg inter text-white text-md mt-3 hover:opacity-95 w-40" hidden>Save and apply</label>
<label id="save_label_dummy" class="button outline-none py-3 px-4 pl-5 rounded-lg bg-slate-300 inter text-white text-md mt-3 w-40">Save and apply</label>
</div>

</form>



{% endblock %}
</body>
<!-- script import -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="{{ url_for('static', filename='js/script.js') }}" type="text/javascript"></script>
</html>
<!-- Path: src/templates/settings.html -->
Binary file modified src/tests/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified src/tests/__pycache__/test_launch.cpython-311-pytest-7.3.1.pyc
Binary file not shown.
Binary file modified tests/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified tests/__pycache__/test_launch.cpython-311-pytest-7.3.1.pyc
Binary file not shown.
1 change: 0 additions & 1 deletion tests/data/settings.json

This file was deleted.

0 comments on commit 738f9d8

Please sign in to comment.