Update app.py
Browse files1234562 unmodified lines9101112131417 unmodified lines3233343536373839404142433 unmodified lines474849505152535455565758596061626364656667686970717273747519 unmodified lines959697989910010110210310410510610721 unmodified lines1291301311321331341351361371381391401411421431441451461471481491501511521536 unmodified lines1601611621631641651666 unmodified lines173174175176import osimport gradio as grfrom openai import OpenAIfrom optillm.cot_reflection import cot_reflectionfrom optillm.rto import round_trip_optimizationfrom optillm.z3_solver import Z3SymPySolverSystem2 unmodified linesfrom optillm.leap import leapfrom optillm.reread import re2_approachAPI_KEY = os.environ.get("OPENROUTER_API_KEY")def compare_responses(message, model1, approach1, model2, approach2, system_message, max_tokens, temperature, top_p):17 unmodified linesinitial_query = "\n".join(conversation)return system_prompt, initial_querydef respond(message, history, model, approach, system_message, max_tokens, temperature, top_p, image=None):try:client = OpenAI(api_key=API_KEY, base_url="https://openrouter.ai/api/v1")messages = [{"role": "system", "content": system_message}]# Add history if availablefor val in history:if val[0]:messages.append({"role": "user", "content": val[0]})3 unmodified linesmessages.append({"role": "user", "content": message})if approach == "none":# Prepare the API request datadata = {"model": model,"messages": messages,"max_tokens": max_tokens,"temperature": temperature,"top_p": top_p,}if image:data["image"] = image # Add image if providedresponse = client.chat.completions.create(extra_headers={"HTTP-Referer": "https://github.com/codelion/optillm","X-Title": "optillm"},**data)return response.choices[0].message.contentelse:system_prompt, initial_query = parse_conversation(messages)# Handle different approachesif approach == 'rto':final_response, _ = round_trip_optimization(system_prompt, initial_query, client, model)elif approach == 'z3':19 unmodified linesdef create_model_dropdown():return gr.Dropdown([ "meta-llama/llama-3.1-8b-instruct:free", "nousresearch/hermes-3-llama-3.1-405b:free", "meta-llama/llama-3.2-1b-instruct:free","mistralai/mistral-7b-instruct:free", "mistralai/pixtral-12b:free", "meta-llama/llama-3.1-70b-instruct:free","qwen/qwen-2-7b-instruct:free", "qwen/qwen-2-vl-7b-instruct:free", "google/gemma-2-9b-it:free", "liquid/lfm-40b:free", "meta-llama/llama-3.1-405b-instruct:free","openchat/openchat-7b:free", "meta-llama/llama-3.2-90b-vision-instruct:free", "meta-llama/llama-3.2-11b-vision-instruct:free","meta-llama/llama-3-8b-instruct:free", "meta-llama/llama-3.2-3b-instruct:free", "microsoft/phi-3-medium-128k-instruct:free","microsoft/phi-3-mini-128k-instruct:free", "huggingfaceh4/zephyr-7b-beta:free"],value="meta-llama/llama-3.1-8b-instruct:free", label="Model")def create_approach_dropdown():21 unmodified linesapproach = create_approach_dropdown()chatbot = gr.Chatbot()msg = gr.Textbox()image = gr.Image(type="pil", label="Upload Image (optional)")with gr.Row():submit = gr.Button("Submit")clear = gr.Button("Clear")def user(user_message, history, uploaded_image):return "", history + [[user_message, None]], uploaded_imagedef bot(history, model, approach, system_message, max_tokens, temperature, top_p, uploaded_image):user_message = history[-1][0]bot_message = respond(user_message, history[:-1], model, approach, system_message, max_tokens, temperature, top_p, image=uploaded_image)history[-1][1] = bot_messagereturn historymsg.submit(user, [msg, chatbot, image], [msg, chatbot, image]).then(bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p, image], chatbot)submit.click(user, [msg, chatbot, image], [msg, chatbot, image]).then(bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p, image], chatbot)clear.click(lambda: None, None, chatbot, queue=False)6 unmodified linescompare_input = gr.Textbox(label="Enter your message for comparison")compare_button = gr.Button("Compare")compare_image = gr.Image(type="pil", label="Upload Image for Comparison")with gr.Row():output1 = gr.Textbox(label="Response 1")6 unmodified lines)if __name__ == "__main__":demo.launch()123456782 unmodified lines1112131415161717 unmodified lines35363738394041424344453 unmodified lines49505152535455565758596061626364656667686919 unmodified lines899091929394959697989910010121 unmodified lines1231241251261271281291301311321331341351361371381391401411421431441451466 unmodified lines1531541551561571586 unmodified lines165166167168import osimport gradio as grfrom openai import OpenAIfrom optillm.cot_reflection import cot_reflectionfrom optillm.rto import round_trip_optimizationfrom optillm.z3_solver import Z3SymPySolverSystem2 unmodified linesfrom optillm.leap import leapfrom optillm.reread import re2_approachAPI_KEY = os.environ.get("OPENROUTER_API_KEY")def compare_responses(message, model1, approach1, model2, approach2, system_message, max_tokens, temperature, top_p):17 unmodified linesinitial_query = "\n".join(conversation)return system_prompt, initial_querydef respond(message, history, model, approach, system_message, max_tokens, temperature, top_p):try:client = OpenAI(api_key=API_KEY, base_url="https://openrouter.ai/api/v1")messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})3 unmodified linesmessages.append({"role": "user", "content": message})if approach == "none":response = client.chat.completions.create(extra_headers={"HTTP-Referer": "https://github.com/codelion/optillm","X-Title": "optillm"},model=model,messages=messages,max_tokens=max_tokens,temperature=temperature,top_p=top_p,)return response.choices[0].message.contentelse:system_prompt, initial_query = parse_conversation(messages)if approach == 'rto':final_response, _ = round_trip_optimization(system_prompt, initial_query, client, model)elif approach == 'z3':19 unmodified linesdef create_model_dropdown():return gr.Dropdown([ "meta-llama/llama-3.1-8b-instruct:free", "nousresearch/hermes-3-llama-3.1-405b:free","meta-llama/llama-3.2-1b-instruct:free","mistralai/mistral-7b-instruct:free","mistralai/pixtral-12b:free","meta-llama/llama-3.1-70b-instruct:free","qwen/qwen-2-7b-instruct:free", "qwen/qwen-2-vl-7b-instruct:free", "google/gemma-2-9b-it:free", "liquid/lfm-40b:free", "meta-llama/llama-3.1-405b-instruct:free","openchat/openchat-7b:free", "meta-llama/llama-3.2-90b-vision-instruct:free", "meta-llama/llama-3.2-11b-vision-instruct:free","meta-llama/llama-3-8b-instruct:free", "meta-llama/llama-3.2-3b-instruct:free", "microsoft/phi-3-medium-128k-instruct:free","microsoft/phi-3-mini-128k-instruct:free", "huggingfaceh4/zephyr-7b-beta:free"],value="nousresearch/hermes-3-llama-3.1-405b:free", label="Model")def create_approach_dropdown():21 unmodified linesapproach = create_approach_dropdown()chatbot = gr.Chatbot()msg = gr.Textbox()with gr.Row():submit = gr.Button("Submit")clear = gr.Button("Clear")def user(user_message, history):return "", history + [[user_message, None]]def bot(history, model, approach, system_message, max_tokens, temperature, top_p):user_message = history[-1][0]bot_message = respond(user_message, history[:-1], model, approach, system_message, max_tokens, temperature, top_p)history[-1][1] = bot_messagereturn historymsg.submit(user, [msg, chatbot], [msg, chatbot]).then(bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p], chatbot)submit.click(user, [msg, chatbot], [msg, chatbot]).then(bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p], chatbot)clear.click(lambda: None, None, chatbot, queue=False)6 unmodified linescompare_input = gr.Textbox(label="Enter your message for comparison")compare_button = gr.Button("Compare")with gr.Row():output1 = gr.Textbox(label="Response 1")6 unmodified lines)if __name__ == "__main__":demo.launch()
app.py+21-29