Spaces:
Paused
Paused
File size: 1,793 Bytes
67f52f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from langchain_openai import ChatOpenAI
import os
from langchain_core.messages import HumanMessage
from langchain_core.runnables import chain
from langchain.prompts.chat import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.output_parsers import JsonOutputParser
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
email_prompt = """
You are an email writer. Use the following input to draft an email:
Input: {input}
Deliver:
1. A complete email.
"""
class Email(BaseModel):
email: str = Field(description= "email")
email_parser = JsonOutputParser(pydantic_object=Email)
@chain
def email_model(inputs: dict) -> str | list[str] | dict:
model = ChatOpenAI(temperature=0.5, model="gpt-4o", max_tokens=1024)
msg = model.invoke(
[HumanMessage(
content=[
{"type": "text", "text": inputs["prompt"]},
{"type": "text", "text": inputs["parser"].get_format_instructions()},
])]
)
return msg.content
def get_email(user_input) -> dict:
parser = email_parser
prompt = email_prompt.format(input=user_input)
intent_chain = email_model | parser
return intent_chain.invoke({'prompt': prompt, 'parser':parser})
import gradio as gr
def process_text(input_text):
output = get_email(input_text)
return output["email"]
# Create the Gradio interface
interface = gr.Interface(
fn=process_text, # Function to process the text
inputs=gr.Textbox(label = "Email Instructions"), # Textbox input for the user
outputs=gr.Textbox(label = "Email"), # Textbox output for the response
title="Email Writer", # Title of the app
# description="Enter email instructions"
)
# Launch the app
interface.launch()
|