ChandraP12330 commited on
Commit
bacdfba
·
verified ·
1 Parent(s): 6a1305b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -2
app.py CHANGED
@@ -55,8 +55,41 @@ context_raw= scene_labels[probs.argmax(-1)]
55
  context= 'the image is depicting scene of '+ context_raw
56
 
57
  ##LLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Generate the caption
60
  if st.button("Generate Caption"):
61
-
62
- st.write()
 
55
  context= 'the image is depicting scene of '+ context_raw
56
 
57
  ##LLM
58
+ GOOGLE_API_KEY = st.text_input("Please enter your GOOGLE GEMINI API KEY", type="password")
59
+ os.environ['GOOGLE_API_KEY'] = GOOGLE_API_KEY
60
+
61
+ from langchain_google_genai import ChatGoogleGenerativeAI
62
+ from langchain.prompts import PromptTemplate
63
+ from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory
64
+ llm = ChatGoogleGenerativeAI(model="gemini-1.0-pro-latest", google_api_key=GOOGLE_API_KEY, temperature=0.2, top_p=1, top_k=1,
65
+ safety_settings={
66
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
67
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
68
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
69
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
70
+
71
+ },
72
+ )
73
+ template="""You are an advanced image captioning AI assistant for surveillance related images.
74
+ Your task is to refine and improve an initial image caption using relevant contextual information provided.
75
+ You will receive two inputs:
76
+ Input 1: {initial_caption} - This is the initial caption for the image, most likely grammatically incorrect
77
+ and incomplete sentence, generated by a separate not so good image captioning model.
78
+ Input 2: {context} - This is the contextual information that provides more details about the background
79
+ Your goal is to take the initial caption and the additional context, and produce a new, refined caption that
80
+ incorporates the contextual details.
81
+ Please do not speculate things which are not provided. The final caption should be grammatically correct.
82
+ Please output only the final caption."""
83
+
84
+ prompt_template = PromptTemplate(
85
+ template=template,
86
+ input_variables=["initial_caption", "context"],
87
+ )
88
+
89
+ prompt=prompt_template.format(initial_caption=initial_caption, context=context)
90
+ response = llm.invoke(prompt)
91
+ final_caption = response.content
92
 
93
  # Generate the caption
94
  if st.button("Generate Caption"):
95
+ st.write(final_caption)