Spaces:
Runtime error
Runtime error
utkuarslan5
commited on
Commit
·
614e0a8
1
Parent(s):
1aabdda
yodafy1
Browse files- app.py +43 -3
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,7 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain.llms import OpenAI
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain.chains.llm import LLMChain
|
5 |
+
from langchain.chains.constitutional_ai.base import ConstitutionalChain
|
6 |
+
from langchain.chains.constitutional_ai.models import ConstitutionalPrinciple
|
7 |
|
|
|
|
|
8 |
|
9 |
+
from dotenv import find_dotenv, load_dotenv
|
10 |
+
load_dotenv(find_dotenv())
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def yodafy(sentence):
|
15 |
+
|
16 |
+
llm = OpenAI(temperature=.8)
|
17 |
+
prompt = PromptTemplate(
|
18 |
+
input_variables=["sentence"],
|
19 |
+
template="Rewrite the sentence inside <> as Master Yoda. Sentence: <{sentence}>",
|
20 |
+
)
|
21 |
+
|
22 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
23 |
+
|
24 |
+
master_yoda_principle = ConstitutionalPrinciple(
|
25 |
+
name='Master Yoda Principle',
|
26 |
+
critique_request='Identify specific ways in which the model\'s response is not in the style of Master Yoda.',
|
27 |
+
revision_request='Please rewrite the model response to be in the style of Master Yoda using his teachings and wisdom.',
|
28 |
+
)
|
29 |
+
|
30 |
+
ethical_principle = ConstitutionalPrinciple(
|
31 |
+
name="Ethical Principle",
|
32 |
+
critique_request="The model should only talk about ethical and legal things.",
|
33 |
+
revision_request="Rewrite the model's output to be both ethical and legal.",
|
34 |
+
)
|
35 |
+
|
36 |
+
constitutional_chain = ConstitutionalChain.from_llm(
|
37 |
+
chain=chain,
|
38 |
+
constitutional_principles=[ethical_principle, master_yoda_principle],
|
39 |
+
llm=llm,
|
40 |
+
verbose=True,
|
41 |
+
)
|
42 |
+
|
43 |
+
return constitutional_chain.run(sentence=sentence)
|
44 |
+
|
45 |
+
|
46 |
+
iface = gr.Interface(fn=yodafy, inputs="text", outputs="text")
|
47 |
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|