Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from langchain.chains import LLMChain, SequentialChain | |
| from langchain.prompts import PromptTemplate | |
| from langchain.llms import OpenAI | |
| # Set your OpenAI API Key | |
| os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") | |
| llm = OpenAI(temperature=0.9) | |
| # Chain 1: Restaurant Name | |
| prompt_template_name = PromptTemplate( | |
| input_variables=['cuisine'], | |
| template="I want to open a restaurant for {cuisine} food. Suggest a fancy name for this." | |
| ) | |
| name_chain = LLMChain(llm=llm, prompt=prompt_template_name, output_key="restaraunt_name") | |
| # Chain 2: Menu Items | |
| prompt_template_items = PromptTemplate( | |
| input_variables=['restaraunt_name'], | |
| template="Suggest me some menu items for {restaraunt_name}." | |
| ) | |
| food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items, output_key="menu_items") | |
| # Combine into SequentialChain | |
| chain = SequentialChain( | |
| chains=[name_chain, food_items_chain], | |
| input_variables=['cuisine'], | |
| output_variables=['restaraunt_name', 'menu_items'] | |
| ) | |
| # Gradio interface | |
| def generate_restaurant_info(cuisine): | |
| result = chain({'cuisine': cuisine}) | |
| return result['restaraunt_name'], result['menu_items'] | |
| iface = gr.Interface( | |
| fn=generate_restaurant_info, | |
| inputs=gr.Textbox(label="Enter Cuisine"), | |
| outputs=[ | |
| gr.Textbox(label="Restaurant Name"), | |
| gr.Textbox(label="Menu Items") | |
| ], | |
| title="Restaurant & Menu Generator", | |
| description="Powered by LangChain + OpenAI" | |
| ) | |
| iface.launch() | |