Update app.py
#4
by jaykin01 - opened
123456782 unmodified lines1112131415161717 unmodified lines35363738394041424344453 unmodified lines49505152535455565758596061626364656667686917 unmodified lines8788899091929394959697989910010110210310410510610710826 unmodified lines1351361371381391401411421431441451461471481491501511521531541551561571586 unmodified lines1651661671681691706 unmodified lines177178179180import 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':17 unmodified lineserror_message = f"Error in respond function: {str(e)}\nType: {type(e).__name__}"print(error_message)# for message in client.chat_completion(# messages,# max_tokens=max_tokens,# stream=True,# temperature=temperature,# top_p=top_p,# ):# token = message.choices[0].delta.content# response += token# yield responsedef 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",26 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()1234562 unmodified lines9101112131417 unmodified lines3233343536373839404142433 unmodified lines474849505152535455565758596061626364656667686970717273747517 unmodified lines9394959697989910010110226 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':17 unmodified lineserror_message = f"Error in respond function: {str(e)}\nType: {type(e).__name__}"print(error_message)def 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",26 unmodified linesapproach = create_approach_dropdown()chatbot = gr.Chatbot()msg = gr.Textbox()image = gr.Image(type="pil", label="Upload Image (optional)", optional=True)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", optional=True)with gr.Row():output1 = gr.Textbox(label="Response 1")6 unmodified lines)if __name__ == "__main__":demo.launch()
app.py+28-32