DvorakInnovation commited on
Commit
3e86f8f
·
verified ·
1 Parent(s): f6c4586

Update recommendations_and_engagement_predict.py

Browse files
recommendations_and_engagement_predict.py CHANGED
@@ -1,127 +1,127 @@
1
- import os
2
-
3
-
4
- import gradio as gr
5
- # from transformers import AutoTokenizer, AutoModelForCausalLM
6
- from sentence_transformers import SentenceTransformer
7
- from datasets import load_dataset
8
- import numpy as np
9
- import torch
10
- from shared_resources import shared_resources
11
- from phi.agent import Agent
12
- from phi.tools.duckduckgo import DuckDuckGo
13
- from phi.agent import Agent, RunResponse
14
- from phi.model.huggingface import HuggingFaceChat
15
-
16
-
17
-
18
- class StoryRecommendationApp:
19
-
20
- def __init__(self):
21
- # self.tokenizer = shared_resources.tokenizer
22
- # self.model = shared_resources.model
23
- self.device = shared_resources.device
24
- self.sentence_transformer = shared_resources.sentence_transformer
25
- self.data = shared_resources.data
26
-
27
- def search(self, query: str, k: int = 3):
28
- """Search for recommended videos based on user input."""
29
- embedded_query = self.sentence_transformer.encode(query)
30
- scores, retrieved_examples = self.data.get_nearest_examples("embeddings", embedded_query, k=k)
31
-
32
- if isinstance(retrieved_examples, np.ndarray):
33
- retrieved_examples = np.nan_to_num(retrieved_examples, nan=0)
34
- elif isinstance(retrieved_examples, list):
35
- retrieved_examples = [
36
- [0 if np.isnan(val) else val for val in example] if isinstance(example, list) else example
37
- for example in retrieved_examples
38
- ]
39
- return scores, retrieved_examples
40
-
41
- def compute_mean_and_predict(self, retrieved_examples):
42
- """Compute mean for likesCount, commentCount, and shareCount."""
43
- features = ["LikesCount", "commentCount", "shareCount"]
44
- predictions = {}
45
-
46
- for feature in features:
47
- values = np.array(retrieved_examples[feature])
48
- values = np.array([np.nan if v is None else v for v in values])
49
- values = np.nan_to_num(values, nan=0.0)
50
-
51
- mean_value = int(np.mean(values))
52
- predictions[f"predicted_{feature}"] = mean_value
53
-
54
- return predictions
55
-
56
- def generate_prompt(self, query: str):
57
- """Generate a prompt for video generation based on the input story."""
58
- input_text = f'''
59
- I want to summarize a story in exactly 3 sentences. No more than 3 nor less than 3.
60
- But the sentences have to be good enough to use as a prompt for video generation, because I have to give those 3 sentences to the video generation model.
61
- For example: This prompt is about A heartwarming family reunion celebrating love and cherished memories in exactly 3 sentences.
62
- -A warm, heartfelt reunion in a cozy living room, where family members embrace each other after a long time apart, soft lighting enhances the intimate atmosphere, and laughter fills the air.
63
- -A close-up shot of a grandmother’s hands carefully arranging a family photo album, as the camera pans over old pictures, evoking cherished memories and a deep sense of love.
64
- -A final moment around the dinner table, family members sharing a meal together, toasts are made, and the soft glow of candles reflects the joy and connection between generations.
65
-
66
- So, I will provide you that story now. The story is:\n{query}
67
-
68
- Please remember, don't give any background descriptions. Just generate 3 sentences likewise the example above. Don't even give the starting text like: "Here are the 3 sentences that summarize the story:" or any other like this. Just give the answers in 3 sentences directly
69
- '''
70
- agent = Agent(
71
- model=HuggingFaceChat(
72
- id="meta-llama/Meta-Llama-3-8B-Instruct",
73
- max_tokens=4096,
74
- ),
75
- # tools=[DuckDuckGo()],
76
- markdown=True
77
- )
78
-
79
- # Get the response in a variable
80
- run: RunResponse = agent.run(input_text)
81
- generated_text=run.content
82
-
83
- sentences = [sentence.strip() for sentence in generated_text.split('.') if sentence]
84
- return '. '.join(sentences[-3:]) + ('.' if len(sentences) > 0 else '')
85
-
86
- def generate_story_and_recommendation(self, generated_response: str):
87
- """Generate story recommendations and predictions based on the user input."""
88
- scores, result = self.search(generated_response, 4)
89
-
90
- recommended_videos_text = ""
91
- predictions = {}
92
-
93
- if scores is not None and result is not None:
94
- recommendations = []
95
- for idx in range(len(result['url'])):
96
- recommendations.append(
97
- f"Video {idx+1}: {result['url'][idx]}\nPlaycount: {int(result['playCount'][idx])}\n"
98
- )
99
- recommended_text = "\n\n".join(recommendations)
100
- recommended_influencer = f"\nYou can use these influencers for this type of video {str(result['username'][:3])}"
101
-
102
- predictions = self.compute_mean_and_predict(result)
103
- generated_prompt = self.generate_prompt(generated_response)
104
-
105
- return recommended_text + recommended_influencer, predictions, generated_prompt
106
-
107
- def format_predictions(self, predictions):
108
- """Format predictions for display."""
109
- if predictions:
110
- return "\n".join([f"{key}: {value}" for key, value in predictions.items()])
111
- else:
112
- return "No predictions available."
113
-
114
- def launch_interface(self):
115
- """Launch the Gradio interface."""
116
- interface=gr.Interface(
117
- fn=self.generate_story_and_recommendation,
118
- inputs=gr.Textbox(label="Enter your generated story.", lines=15),
119
- outputs=[
120
- gr.Textbox(label="Our Recommendations for you."),
121
- gr.Textbox(label="Predicted Metrics (Likes, Comments, Shares)", type="text"),
122
- gr.Textbox(label="Recommended Prompt for video generation:"),
123
- ],
124
- title="Video Story Generation and Recommendation",
125
- description="Enter a request for a video storyline, and get a detailed story along with recommended videos and predicted engagement metrics based on the same input."
126
- )
127
- return interface
 
1
+ import os
2
+
3
+
4
+ import gradio as gr
5
+ # from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from sentence_transformers import SentenceTransformer
7
+ from datasets import load_dataset
8
+ import numpy as np
9
+ import torch
10
+ from shared_resources import shared_resources
11
+ from phi.agent import Agent
12
+ from phi.tools.duckduckgo import DuckDuckGo
13
+ from phi.agent import Agent, RunResponse
14
+ from phi.model.huggingface import HuggingFaceChat
15
+
16
+
17
+
18
+ class StoryRecommendationApp:
19
+
20
+ def __init__(self):
21
+ # self.tokenizer = shared_resources.tokenizer
22
+ # self.model = shared_resources.model
23
+ self.device = shared_resources.device
24
+ self.sentence_transformer = shared_resources.sentence_transformer
25
+ self.data = shared_resources.data
26
+
27
+ def search(self, query: str, k: int = 3):
28
+ """Search for recommended videos based on user input."""
29
+ embedded_query = self.sentence_transformer.encode(query)
30
+ scores, retrieved_examples = self.data.get_nearest_examples("embeddings", embedded_query, k=k)
31
+
32
+ if isinstance(retrieved_examples, np.ndarray):
33
+ retrieved_examples = np.nan_to_num(retrieved_examples, nan=0)
34
+ elif isinstance(retrieved_examples, list):
35
+ retrieved_examples = [
36
+ [0 if np.isnan(val) else val for val in example] if isinstance(example, list) else example
37
+ for example in retrieved_examples
38
+ ]
39
+ return scores, retrieved_examples
40
+
41
+ def compute_mean_and_predict(self, retrieved_examples):
42
+ """Compute mean for likesCount, commentCount, and shareCount."""
43
+ features = ["LikesCount", "commentCount", "shareCount"]
44
+ predictions = {}
45
+
46
+ for feature in features:
47
+ values = np.array(retrieved_examples[feature])
48
+ values = np.array([np.nan if v is None else v for v in values])
49
+ values = np.nan_to_num(values, nan=0.0)
50
+
51
+ mean_value = int(np.mean(values))
52
+ predictions[f"predicted_{feature}"] = mean_value
53
+
54
+ return predictions
55
+
56
+ def generate_prompt(self, query: str):
57
+ """Generate a prompt for video generation based on the input story."""
58
+ input_text = f'''
59
+ I want to summarize a story in exactly 3 sentences. No more than 3 nor less than 3.
60
+ But the sentences have to be good enough to use as a prompt for video generation, because I have to give those 3 sentences to the video generation model.
61
+ For example: This prompt is about A heartwarming family reunion celebrating love and cherished memories in exactly 3 sentences.
62
+ -A warm, heartfelt reunion in a cozy living room, where family members embrace each other after a long time apart, soft lighting enhances the intimate atmosphere, and laughter fills the air.
63
+ -A close-up shot of a grandmother’s hands carefully arranging a family photo album, as the camera pans over old pictures, evoking cherished memories and a deep sense of love.
64
+ -A final moment around the dinner table, family members sharing a meal together, toasts are made, and the soft glow of candles reflects the joy and connection between generations.
65
+
66
+ So, I will provide you that story now. The story is:\n{query}
67
+
68
+ Please remember, don't give any background descriptions. Just generate 3 sentences likewise the example above. Don't even give the starting text like: "Here are the 3 sentences that summarize the story:" or any other like this. Just give the answers in 3 sentences directly
69
+ '''
70
+ agent = Agent(
71
+ model=HuggingFaceChat(
72
+ id="meta-llama/Llama-3.2-1B-Instruct-SpinQuant_INT4_EO8",
73
+ max_tokens=4096,
74
+ ),
75
+ # tools=[DuckDuckGo()],
76
+ markdown=True
77
+ )
78
+
79
+ # Get the response in a variable
80
+ run: RunResponse = agent.run(input_text)
81
+ generated_text=run.content
82
+
83
+ sentences = [sentence.strip() for sentence in generated_text.split('.') if sentence]
84
+ return '. '.join(sentences[-3:]) + ('.' if len(sentences) > 0 else '')
85
+
86
+ def generate_story_and_recommendation(self, generated_response: str):
87
+ """Generate story recommendations and predictions based on the user input."""
88
+ scores, result = self.search(generated_response, 4)
89
+
90
+ recommended_videos_text = ""
91
+ predictions = {}
92
+
93
+ if scores is not None and result is not None:
94
+ recommendations = []
95
+ for idx in range(len(result['url'])):
96
+ recommendations.append(
97
+ f"Video {idx+1}: {result['url'][idx]}\nPlaycount: {int(result['playCount'][idx])}\n"
98
+ )
99
+ recommended_text = "\n\n".join(recommendations)
100
+ recommended_influencer = f"\nYou can use these influencers for this type of video {str(result['username'][:3])}"
101
+
102
+ predictions = self.compute_mean_and_predict(result)
103
+ generated_prompt = self.generate_prompt(generated_response)
104
+
105
+ return recommended_text + recommended_influencer, predictions, generated_prompt
106
+
107
+ def format_predictions(self, predictions):
108
+ """Format predictions for display."""
109
+ if predictions:
110
+ return "\n".join([f"{key}: {value}" for key, value in predictions.items()])
111
+ else:
112
+ return "No predictions available."
113
+
114
+ def launch_interface(self):
115
+ """Launch the Gradio interface."""
116
+ interface=gr.Interface(
117
+ fn=self.generate_story_and_recommendation,
118
+ inputs=gr.Textbox(label="Enter your generated story.", lines=15),
119
+ outputs=[
120
+ gr.Textbox(label="Our Recommendations for you."),
121
+ gr.Textbox(label="Predicted Metrics (Likes, Comments, Shares)", type="text"),
122
+ gr.Textbox(label="Recommended Prompt for video generation:"),
123
+ ],
124
+ title="Video Story Generation and Recommendation",
125
+ description="Enter a request for a video storyline, and get a detailed story along with recommended videos and predicted engagement metrics based on the same input."
126
+ )
127
+ return interface