Abdelmageed commited on
Commit
918cd3a
1 Parent(s): 4610644

Add application file

Browse files
Files changed (2) hide show
  1. app.py +117 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer ,T5ForConditionalGeneration ,T5Tokenizer
2
+ import re
3
+ import torch
4
+ torch.set_default_tensor_type(torch.cuda.FloatTensor)
5
+ import os
6
+ import io
7
+ import warnings
8
+ from PIL import Image
9
+ from stability_sdk import client
10
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
11
+ import gradio as gr
12
+
13
+
14
+ def generate_post(model,tokenizer,company_name , description , example1 ,example2 ,example3):
15
+ prompt = f""" {company_name} {description}, {example1}.
16
+ {company_name} {description}, {example2}.
17
+ {company_name} {description}, {example3}.
18
+ {company_name} {description}, """
19
+
20
+ input_ids = tokenizer(prompt, return_tensors="pt").to(0)
21
+ sample = model.generate(**input_ids, top_k=0, temperature=0.7, do_sample = True , max_new_tokens = 70, repetition_penalty= 5.4)
22
+ outputs = tokenizer.decode(sample[0])
23
+ res = outputs.split(f""" {company_name} {description}, {example1}.
24
+ {company_name} {description}, {example2}.
25
+ {company_name} {description}, {example3}.
26
+ {company_name} {description}, """)[1]
27
+
28
+ res = re.sub('[#]\w+' , " ", res)
29
+ res = re.sub('@[^\s]\w+',' ', res)
30
+ res = re.sub(r'http\S+', ' ', res)
31
+ res = res.replace("\n" ," ")
32
+ res = re.sub(' +', ' ',res)
33
+ return res
34
+
35
+ def generate_caption(model , text_body ,tokenizer ,max_length):
36
+ test_sent = 'generate: ' + text_body
37
+ input = tokenizer.encode(test_sent , return_tensors="pt")#.to('cuda')
38
+ outs = model.generate(input ,
39
+ max_length = max_length,
40
+ do_sample = True ,
41
+ temperature = 0.7,
42
+ min_length = 8,
43
+ repetition_penalty = 5.4,
44
+ max_time = 12,
45
+ top_p = 1.0,
46
+ top_k = 50)
47
+
48
+
49
+ sent = tokenizer.decode(outs[0], skip_special_tokens=True,clean_up_tokenization_spaces=True)
50
+ return sent
51
+
52
+
53
+
54
+ def demo_smg(company_name ,description , example1 , example2 , example3):
55
+
56
+ model_cp= T5ForConditionalGeneration.from_pretrained("Abdelmageed95/caption_model")
57
+ tokenizer = T5Tokenizer.from_pretrained('t5-base')
58
+
59
+ access_token = "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"
60
+ model_bm = AutoModelForCausalLM.from_pretrained("bigscience/bloom-3b" , use_auth_token = access_token)
61
+ tokenizer_bm = AutoTokenizer.from_pretrained("bigscience/bloom-3b")
62
+
63
+ res = generate_post( model_bm , tokenizer_bm, company_name , description , example1 , example2 , example3)
64
+ generated_caption = generate_caption( model_cp ,
65
+ res,
66
+ tokenizer ,
67
+ 30)
68
+
69
+ os.environ['STABILITY_HOST'] = "grpc.stability.ai:443"
70
+ os.environ['STABILITY_KEY'] = "sk-t4x1wv6WFgTANF7O1TkWDJZzxXxQZeU6X7oybl6rdCOOiHIk"
71
+ stability_api = client.StabilityInference(
72
+ key=os.environ['STABILITY_KEY'],
73
+ verbose=True)
74
+ generated_caption = generated_caption + ", intricate, highly detailed, smooth , sharp focus, 8k"
75
+
76
+ answers = stability_api.generate( prompt= generated_caption ,
77
+ #seed=34567,
78
+ steps= 70 )
79
+
80
+
81
+ for resp in answers:
82
+ for artifact in resp.artifacts:
83
+ if artifact.finish_reason == generation.FILTER:
84
+ warnings.warn(
85
+ "Your request activated the API's safety filters and could not be processed."
86
+ "Please modify the prompt and try again.")
87
+ if artifact.type == generation.ARTIFACT_IMAGE:
88
+ img = Image.open(io.BytesIO(artifact.binary))
89
+
90
+
91
+ return res, generated_caption ,img
92
+
93
+
94
+ company_name = "ADES Group"
95
+
96
+ description = "delivers full-scale petroleum services; from onshore and offshore drilling to full oil & gas projects and services, with emphasis on the HSE culture while maintaining excellence in operation targets."
97
+
98
+ example1 = """Throwback to ADM 680 Team during their Cyber-chair controls Course in August,
99
+ Our development strategy at ADES does not only focus on enriching the technical expertise of our teams in their specialization in Jack- up rigs,
100
+ but also in providing access to latest operational models"""
101
+
102
+ example2 = """With complexity of oil & gas equipment and the seriousness of failure and its consequences confronting our people,
103
+ it has become a necessity to equip our Asset Management Team with leading methodologies and techniques that enable them to think and act proactively"""
104
+
105
+ example3 = """ Part of our people development strategy is providing our senior leadership with the latest industry technologies
106
+ and world class practices and standards"""
107
+ txt , generated_caption , im = demo_smg( company_name, description , example1 , example2 , example3)
108
+
109
+ print(txt)
110
+ print(generated_caption)
111
+
112
+ # demo = gr.Interface(
113
+ # fn= demo_smg,
114
+ # inputs=["text","text" , "text" ,"text" ,"text"],
115
+ # outputs=["text", "text", "image" ]
116
+ # )
117
+ # demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers -q
2
+ gradio==3.3.1
3
+ SentencePiece==0.1.97
4
+ stability-sdk==0.2.3