awacke1 commited on
Commit
bf4783e
1 Parent(s): da31f5c

Create new file

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # GPT-J-6B API
5
+ API_URL = "https://api-inference.huggingface.co/models/EleutherAI/gpt-j-6B"
6
+ headers = {"Authorization": "Bearer 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"}
7
+ prompt = """Customer: Hi, this is M. Davenport, how may I direct your call?
8
+ Agent: Thankyou, today I seek some Wellness and Mindfulness advice.
9
+ Customer: Great! I've been searching for good solutions to enhance memory and health.
10
+ Agent: Let me share some of the resources with you including mnemonics, agents, nutrition, exercise, and good choices"""
11
+
12
+ examples = [["mind"], ["memory"], ["sleep"],["wellness"],["nutrition"],["mnemonics"]]
13
+
14
+
15
+ def poem2_generate(word):
16
+ p = word.lower() + "\n" + "poem using word: "
17
+ print(f"*****Inside poem_generate - Prompt is :{p}")
18
+ json_ = {"inputs": p,
19
+ "parameters":
20
+ {
21
+ "top_p": 0.9,
22
+ "temperature": 1.1,
23
+ "max_new_tokens": 50,
24
+ "return_full_text": False
25
+ }}
26
+ response = requests.post(API_URL, headers=headers, json=json_)
27
+ output = response.json()
28
+ print(f"If there was an error? Reason is : {output}")
29
+ output_tmp = output[0]['generated_text']
30
+ print(f"GPTJ response without splits is: {output_tmp}")
31
+ #poem = output[0]['generated_text'].split("\n\n")[0] # +"."
32
+ if "\n\n" not in output_tmp:
33
+ if output_tmp.find('.') != -1:
34
+ idx = output_tmp.find('.')
35
+ poem = output_tmp[:idx+1]
36
+ else:
37
+ idx = output_tmp.rfind('\n')
38
+ poem = output_tmp[:idx]
39
+ else:
40
+ poem = output_tmp.split("\n\n")[0] # +"."
41
+ poem = poem.replace('?','')
42
+ print(f"Poem being returned is: {poem}")
43
+ return poem
44
+
45
+
46
+ def poem_generate(word):
47
+
48
+ p = prompt + word.lower() + "\n" + "poem using word: "
49
+ print(f"*****Inside poem_generate - Prompt is :{p}")
50
+ json_ = {"inputs": p,
51
+ "parameters":
52
+ {
53
+ "top_p": 0.9,
54
+ "temperature": 1.1,
55
+ "max_new_tokens": 50,
56
+ "return_full_text": False
57
+ }}
58
+ response = requests.post(API_URL, headers=headers, json=json_)
59
+ output = response.json()
60
+ print(f"If there was an error? Reason is : {output}")
61
+ output_tmp = output[0]['generated_text']
62
+ print(f"GPTJ response without splits is: {output_tmp}")
63
+ #poem = output[0]['generated_text'].split("\n\n")[0] # +"."
64
+ if "\n\n" not in output_tmp:
65
+ if output_tmp.find('.') != -1:
66
+ idx = output_tmp.find('.')
67
+ poem = output_tmp[:idx+1]
68
+ else:
69
+ idx = output_tmp.rfind('\n')
70
+ poem = output_tmp[:idx]
71
+ else:
72
+ poem = output_tmp.split("\n\n")[0] # +"."
73
+ poem = poem.replace('?','')
74
+ print(f"Poem being returned is: {poem}")
75
+ return poem
76
+
77
+ def poem_to_image(poem):
78
+ print("*****Inside Poem_to_image")
79
+ poem = " ".join(poem.split('\n'))
80
+ poem = poem + " oil on canvas."
81
+ steps, width, height, images, diversity = '50','256','256','1',15
82
+ img = gr.Interface.load("spaces/multimodalart/latentdiffusion")(poem, steps, width, height, images, diversity)[0]
83
+ return img
84
+
85
+ demo = gr.Blocks()
86
+
87
+ with demo:
88
+ gr.Markdown("<h1><center>Few Shot Learning for Text - Word Image Search</center></h1>")
89
+ gr.Markdown(
90
+ "<div>This example uses prompt engineering to search for answers in EleutherAI large language model and follows the pattern of Few Shot Learning where you supply A 1) Task Description, 2) a Set of Examples, and 3) a Prompt. Then few shot learning can show the answer given the pattern of the examples. More information on how it works is here: https://huggingface.co/blog/few-shot-learning-gpt-neo-and-inference-api Also the Eleuther AI was trained on texts called The Pile which is documented here on its github. Review this to find what types of language patterns it can generate text for as answers: https://github.com/EleutherAI/the-pile"
91
+ )
92
+ with gr.Row():
93
+ input_word = gr.Textbox(lines=7, value=prompt)
94
+ poem_txt = gr.Textbox(lines=7)
95
+ output_image = gr.Image(type="filepath", shape=(256,256))
96
+
97
+ b1 = gr.Button("Generate Text")
98
+ b2 = gr.Button("Generate Image")
99
+
100
+ b1.click(poem2_generate, input_word, poem_txt)
101
+ b2.click(poem_to_image, poem_txt, output_image)
102
+ #examples=examples
103
+
104
+ demo.launch(enable_queue=True)