Ubaidbhat commited on
Commit
67f52f6
1 Parent(s): a211efc

Create app.py

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