DexterSptizu commited on
Commit
ea967b2
·
verified ·
1 Parent(s): 2b4c6e0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #https://python.langchain.com/docs/how_to/binding/
2
+ import gradio as gr
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+
6
+ def process_with_binding(text, stop_word, api_key):
7
+ try:
8
+ # Initialize the model
9
+ model = ChatOpenAI(
10
+ openai_api_key=api_key,
11
+ model="gpt-4o-mini"
12
+ )
13
+
14
+ # Create prompt template
15
+ prompt = ChatPromptTemplate.from_template(
16
+ "Write a story about {text}. Make it detailed."
17
+ )
18
+
19
+ # Create chain with binding
20
+ chain = prompt | model.bind(stop=stop_word)
21
+
22
+ # Get response
23
+ response = chain.invoke({"text": text})
24
+
25
+ return response.content
26
+
27
+ except Exception as e:
28
+ return f"Error: {str(e)}"
29
+
30
+ # Create Gradio interface
31
+ demo = gr.Interface(
32
+ fn=process_with_binding,
33
+ inputs=[
34
+ gr.Textbox(label="Topic for story", placeholder="Enter a topic..."),
35
+ gr.Textbox(label="Stop Word", placeholder="Enter a word to stop generation"),
36
+ gr.Textbox(label="OpenAI API Key", type="password")
37
+ ],
38
+ outputs=gr.Textbox(label="Generated Story"),
39
+ title="LangChain Binding Demo",
40
+ description="Generate a story that stops at a specific word"
41
+ )
42
+
43
+ if __name__ == "__main__":
44
+ demo.launch()