| import os |
| import base64 |
|
|
| from xai_sdk import Client |
| from xai_sdk.chat import user, image |
| from dotenv import load_dotenv |
| import cv2 |
| import json |
| from create_captions import create_caps |
| load_dotenv() |
|
|
| def encode_image_to_base64_f(frame): |
| """ |
| Encode an OpenCV frame (NumPy array) to a base64 string. |
| |
| Args: |
| frame (np.ndarray): OpenCV frame in BGR format. |
| |
| Returns: |
| str: Base64-encoded string of the image with data URL prefix, or None if encoding fails. |
| """ |
| try: |
| |
| _, buffer = cv2.imencode('.jpg', frame) |
| |
| encoded_string = base64.b64encode(buffer).decode('utf-8') |
| |
| return f"data:image/jpeg;base64,{encoded_string}" |
| except Exception as e: |
| print(f"Error encoding frame to base64: {str(e)}") |
| return None |
|
|
| def analyze_image_with_grok_f( frame): |
| """ |
| Analyze an image using Grok via xAI API. |
| |
| Args: |
| api_key (str): xAI API key. |
| image_path (str): Path to the local image file. |
| |
| Returns: |
| str: Grok's analysis of the image or error message. |
| """ |
| |
| api_key = os.getenv("X_AI") |
| client = Client( |
| api_key=api_key, |
| ) |
| |
| |
| base64_image = encode_image_to_base64_f(frame) |
| if not base64_image: |
| return "Error: Could not encode image. Check if the file exists and is valid." |
| |
| |
| chat = client.chat.create(model="grok-4-0709") |
| |
| |
| chat.append( |
| user( |
| "Write a short and concise description of the image", |
| image(base64_image) |
| ) |
| ) |
| |
| try: |
| response = chat.sample() |
| return response.content |
| except Exception as e: |
| return f"Error during API call: {str(e)}" |
|
|
| def get_story_with_grok(metadata,current_context,dialog): |
| |
| api_key = os.getenv("X_AI") |
| client = Client( |
| api_key=api_key, |
| ) |
| |
| chat = client.chat.create(model="grok-4-0709") |
| print(current_context) |
| if current_context!="": |
| |
| chat.append( |
| user( |
| |
| "Based on the following metadata,the phrase: "+current_context+ ", and the given dialog, generate an engaging story with no more than 4 scenes. The story should be given as JSON, with the scenes in the following format:scenes:[path:file_path,start:,end:,description:].The path is extracted from the metadata file. Do not include a title, a main description, or anything else besides the scenes in the story as JSON format. The metadata: " + str(metadata) + " And the following dialog: "+ dialog, |
|
|
| )) |
| else: |
| print("here") |
| chat.append( |
| user( |
| |
| "You are a vlogger, making videos about your life and your adventures. Based on the following metadata and the given dialog, generate an engaging story with no more than 4 scenes related to the metadata. Use less adjectives with short sentences. The story should be given as JSON, with the scenes in the following format:scenes:[path:file_path,start:,end:,description:].The path is extracted from the metadata file. Do not include a title, a main description, or anything else besides the scenes in the story as JSON format. The metadata: " + str(metadata) + " And the following dialog: "+ dialog, |
| |
| ) |
| ) |
| |
| try: |
| response = chat.sample() |
| data= response.content |
| video_data = json.loads(data) |
| print(video_data) |
| return video_data['scenes'] |
| except Exception as e: |
| return f"Error during API call: {str(e)}" |
|
|
|
|
|
|
| def create_caps_with_grok(steps): |
| api_key = os.getenv("X_AI") |
| client = Client( |
| api_key=api_key, |
| ) |
| |
| for step in steps: |
| chat = client.chat.create(model="grok-4-0709") |
| chat.append( |
| user( |
| "You are a vlogger, making videos about your life. Based on the following description generate a short caption, with no more than three short sentences, less adjectives, in present tense, first person that is fit for a story. Do not include a title or anython else besides the final caption. the description: "+step["description"], |
| )) |
| try: |
| response = chat.sample() |
| data= response.content |
| step["description"]=data |
| except Exception as e: |
| return f"Error during API call: {str(e)}" |
| return steps |
|
|
|
|
|
|
| def main(): |
|
|
| context_path="/Users/georgia.bucea/products/ShortsAI/all_files_metadata.json" |
| with open(context_path, 'r') as file: |
|
|
| data = json.load(file) |
| print("here we are") |
| steps=get_story_with_grok(data,"") |
| print(steps) |
| steps=create_caps_with_grok(steps) |
| print(steps) |
| |
| |
|
|
| if __name__ == "__main__": |
| main() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|