| import os |
| import openai |
| import logging |
| import json |
| from dotenv import load_dotenv |
| import gradio as gr |
| import numpy as np |
| from sklearn.metrics.pairwise import cosine_similarity |
|
|
| from langsmith import traceable |
| from langsmith.wrappers import wrap_openai |
| |
| client = wrap_openai(openai.Client()) |
| |
| load_dotenv() |
| |
| openai.api_key = os.getenv("OPENAI_API_KEY") |
| if not openai.api_key: |
| raise ValueError("Error: OPENAI_API_KEY not found in environment variables") |
|
|
| openai.model = os.getenv("OPENAI_MODEL_NAME") |
|
|
| |
| |
| def ydcoza_cost(tokens_out): |
| t_cost = tokens_out * 6.2 / 1000000 |
| return t_cost |
|
|
| |
| |
| def get_prompts(): |
| local_file_path = "custom_prompts/hormozi_prompt_vector_storage.md" |
| try: |
| with open(local_file_path, 'r') as file: |
| prompt = file.read() |
| except FileNotFoundError: |
| print(f"Prompt not found: {local_file_path}") |
| prompt = "" |
| except Exception as e: |
| print(f"An error occurred: {e}") |
| prompt = "" |
| return prompt |
|
|
| |
| |
| with open('summary_sections.json', 'r') as file: |
| summary_sections = json.load(file) |
|
|
| |
| for section in summary_sections: |
| response = client.embeddings.create( |
| model="text-embedding-3-small", |
| input=section["content"] |
| ) |
| section["embedding"] = response.data[0].embedding |
|
|
| |
| knowledge_base = summary_sections |
|
|
| |
| |
| def find_most_similar(query_embedding, knowledge_base, top_k=1): |
| """ |
| Finds the most similar entries in the knowledge base to the query embedding. |
| Returns the top_k most similar entries. |
| """ |
| embeddings = [entry["embedding"] for entry in knowledge_base] |
| similarities = cosine_similarity([query_embedding], embeddings) |
| most_similar_indices = np.argsort(similarities[0])[::-1][:top_k] |
| return [knowledge_base[idx] for idx in most_similar_indices] |
|
|
| |
| |
| def reformulate_input(user_input): |
| |
| ydcoza_prompt_input = f"# IDENTITY and PURPOSE\nYou are a writing expert. You refine the input text to enhance clarity, coherence and structure\n\n# Steps\nYou are presented with a business idea if the idea is not well structured / formulated, improve on it\n\n# OUTPUT INSTRUCTIONS\n- Write a short description not exceeding 170 words or 1200 character. Make use of bulletpoints to highlight key information\n - NOTE: You are not to give ideas or suggestion just write an improved description of the proposed business idea.\n - Output in markdown format" |
|
|
| |
| response = client.chat.completions.create( |
| model="gpt-4o-mini", |
| messages=[ |
| {"role": "system", "content": ydcoza_prompt_input}, |
| {"role": "user", "content": user_input} |
| ] |
| ) |
| reformulated_input = response.choices[0].message.content |
| return f"### The Improved Description\n\n" + reformulated_input |
|
|
| |
| |
| def generate_openai_response(reformulated): |
| logging.info("Generating response from OpenAI API for the prompt") |
|
|
| |
| query_response = client.embeddings.create( |
| model="text-embedding-3-small", |
| input=reformulated |
| ) |
| query_embedding = query_response.data[0].embedding |
|
|
| |
| relevant_content = find_most_similar(query_embedding, knowledge_base, top_k=1) |
| relevant_text = relevant_content[0]['content'] |
|
|
| |
| prompt = get_prompts() |
| full_prompt = f"""{prompt}\n\n#USER INPUT\n\n#RELEVANT INFORMATION\n{relevant_text}""" |
|
|
| |
| messages = [ |
| {"role": "system", "content": full_prompt}, |
| {"role": "user", "content": reformulated} |
| ] |
|
|
| try: |
| response = client.chat.completions.create( |
| model=openai.model, |
| messages=messages, |
| temperature=1, |
| top_p=1, |
| frequency_penalty=0, |
| presence_penalty=0 |
| ) |
| assistant_message = response.choices[0].message.content |
| tokens_used = response.usage.total_tokens |
| cost_per_call = ydcoza_cost(tokens_used) |
|
|
| |
| return assistant_message, f"<p class='cost_per_call'>Tokens Consumed: {tokens_used} ; Cost per call: ${cost_per_call:.6f}</p>" |
|
|
| except Exception as e: |
| logging.error(f"Error generating OpenAI response: {e}") |
| return f"Error occurred: {str(e)}", "" |
|
|
| |
| |
|
|
| |
| def start_process(user_input): |
| reformulated = reformulate_input(user_input) |
| return reformulated, gr.update(visible=False), gr.update(visible=True), gr.update(visible=True) |
|
|
| |
| def continue_process(reformulated): |
| final_output, cost_message = generate_openai_response(reformulated) |
| return final_output, cost_message, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False) |
|
|
| |
| |
| def load_auth_users(file_path): |
| auth_users = [] |
| try: |
| with open(file_path, 'r') as file: |
| lines = file.readlines() |
| for line in lines: |
| |
| line = line.strip() |
| if line: |
| username, password = line.split(',') |
| auth_users.append((username.strip(), password.strip())) |
| except FileNotFoundError: |
| print(f"Error: File not found - {file_path}") |
| except Exception as e: |
| print(f"An error occurred while loading auth users: {e}") |
| return auth_users |
|
|
| |
| auth_users_file = "auth_users.txt" |
|
|
| |
| auth_users = load_auth_users(auth_users_file) |
|
|
| if not auth_users: |
| raise ValueError("No valid users found. Please check your auth_users.txt file.") |
| |
|
|
| |
| |
| with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Ubuntu"), "Arial", "sans-serif"],text_size='sm')) as ydcoza_face: |
|
|
| with gr.Row(): |
| |
| text_input = gr.Textbox(lines=5, label="Tell me about your business idea, what you have in mind etc.", placeholder="eg. I want to open an Ice cream factory in Santorini Greece ...") |
|
|
| |
| with gr.Row(): |
| examples = gr.Examples(examples=["I want to open an Ice Cream Factory in Piraeus Greece", "If I'm interested in clothing and sales, I might consider starting an online reseller business. Although it takes time, dedication, and an eye for fashion, I can start my business as a side hustle and eventually turn it into a full-time resale business. I could begin by using online store websites like Poshmark and Mercari to sell my unwanted clothing and items, then expand to my own resale website.", "The demand for online education has opened up possibilities for me as an entrepreneur. Since this is an online venture, I can choose any subject I know about and teach a course regardless of my location. If I don’t have advanced knowledge in any particular subject, I could consider teaching English as a foreign language online to students in China", "If I enjoy cleaning, I can easily turn that into a business. With a few staff members, a variety of cleaning supplies, and transportation, I can offer cleaning services to homeowners, apartment complexes, and commercial properties. Most cleaning services charge between $25 to $50 per hour. These services are straightforward businesses that require relatively little overhead; I simply need planning, dedication, and marketing to attract customers."],example_labels=["Short Idea","Clothing & Sales","Online Education","Cleaning Busines"], label="Demo Business Ideas",inputs=[text_input]) |
|
|
| |
| with gr.Row(): |
| big_block_01 = gr.HTML(""" |
| <span class="ydcoza_improve_on_description">We now have a polished version of your description, we'll craft a compelling Hormozi Offer from it.</span> |
| """, visible=False) |
| |
| |
| with gr.Row(): |
| start_button = gr.Button("Let's Start The Process", elem_id='ydcoza_gradio_button', interactive=False) |
| |
| |
| with gr.Row(): |
| desc_output = gr.Markdown(label="Improved Description", elem_id='ydcoza_markdown_output_desc') |
|
|
| |
| with gr.Row(): |
| big_block_03 = gr.HTML(""" |
| <span class="ydcoza_hormozi_offer_out">Awesome! Here's your Hormozi-Styled Offer. I hope it sparks some inspiration and wish you the best of luck with your venture!"</span> |
| """, visible=False) |
| |
| |
| with gr.Row(): |
| markdown_output = gr.Markdown(label="Response", elem_id='ydcoza_markdown_output', visible=False) |
| |
| |
| with gr.Row(): |
| html_output_cost = gr.HTML(elem_id='ydcoza_cost_output', visible=False) |
|
|
| |
| |
| |
| |
| def update_button_state(text): |
| if text.strip(): |
| return gr.update(interactive=True) |
| else: |
| return gr.update(interactive=False) |
|
|
| text_input.change(fn=update_button_state, inputs=text_input, outputs=start_button) |
|
|
| |
| start_button.click(start_process, inputs=[text_input], outputs=[desc_output, start_button, markdown_output, big_block_01]).then( |
| continue_process, inputs=[desc_output], outputs=[markdown_output, html_output_cost, big_block_03, html_output_cost, start_button] |
| ) |
|
|
| |
| if __name__ == "__main__": |
| |
| ydcoza_face.launch() |