File size: 12,381 Bytes
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
6ce680d
df50acb
7073ca0
 
 
 
 
 
 
 
6ce680d
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e415d99
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e314026
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
e415d99
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f02b6a
 
 
 
 
 
7073ca0
2f02b6a
 
 
 
7073ca0
2f02b6a
7073ca0
2f02b6a
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e415d99
 
7073ca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b79cc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from gtts import gTTS
import shutil
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import JSONFormatter
from urllib.parse import urlparse, parse_qs
from pypdf import PdfReader
from ai71 import AI71
import os

AI71_API_KEY = "api71-api-652e5c6c-8edf-41d0-9c34-28522b07bef9"


def extract_text_from_pdf_s(pdf_path):
    text = ""
    reader = PdfReader(pdf_path)
    for page in reader.pages:
        text += page.extract_text() + "\n"
    generate_speech_from_pdf(text[:len(text) // 2])
   
    return text


def generate_response_from_pdf(query, pdf_text):
    response = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a pdf questioning assistant."},
                {"role": "user",
                 "content": f'''Answer the querry based on the given content.Content:{pdf_text},query:{query}'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            response += chunk.choices[0].delta.content
    return response.replace("###", '')


def generate_quiz(subject, topic, count, difficult):
    quiz_output = ""

    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a teaching assistant."},
                {"role": "user",
                 "content": f'''Generate {count} multiple-choice questions in the subject of {subject} for the topic {topic} for students at a {difficult} level. Ensure the questions are well-diversified and cover various aspects of the topic. Format the questions as follows:
Question: [Question text] [specific concept in a question] 
<<o>> [Option1] 
<<o>> [Option2] 
<<o>> [Option3] 
<<o>> [Option4], 
Answer: [Option number]'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            quiz_output += chunk.choices[0].delta.content
    print("Quiz generated")
    return quiz_output



def generate_ai_response(query):
    ai_response = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a teaching assistant."},
                {"role": "user", "content": f'Assist the user clearly for his questions: {query}.'},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            ai_response += chunk.choices[0].delta.content
    return ai_response.replace('###', '')


def generate_project_idea(subject, topic, overview):
    string = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a project building assistant."},
                {"role": "user",
                 "content": f'''Give the different project ideas to build project in {subject} specifically in {topic} for school students. {overview}.'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            string += chunk.choices[0].delta.content
    return string


def generate_project_idea_questions(project_idea, query):
    project_idea_answer = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a project building assistant."},
                {"role": "user",
                 "content": f'''Assist me clearly for the following question for the given idea. Idea: {project_idea}. Question: {query}'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            project_idea_answer += chunk.choices[0].delta.content
    return project_idea_answer


def generate_step_by_step_explanation(query):
    explanation = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are the best teaching assistant."},
                {"role": "user",
                 "content": f'''Provide me the clear step by step explanation answer for the following question. Question: {query}'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            explanation += chunk.choices[0].delta.content
    return explanation.replace('###', '')


def study_plan(subjects, hours, arealag, goal):
    plan = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are the best teaching assistant."},
                {"role": "user",
                 "content": f'''Provide me the clear personalised study plan for the subjects {subjects} i lag in areas like {arealag}, im available for {hours} hours per day and my study goal is to {goal}.Provide me like a timetable like day1,day2 for 5 days with concepts,also suggest some books'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            plan += chunk.choices[0].delta.content
    return plan.replace('\n', '<br>')


class ConversationBufferMemory:
    def __init__(self, memory_key="chat_history"):
        self.memory_key = memory_key
        self.buffer = []

    def add_to_memory(self, interaction):
        self.buffer.append(interaction)

    def get_memory(self):
        return "\n".join([f"Human: {entry['user']}\nAssistant: {entry['assistant']}" for entry in self.buffer])


def spk_msg(user_input, memory):
    chat_history = memory.get_memory()
    msg = ''

    # Construct the message for the API request
    messages = [
        {"role": "system",
         "content": "You are a nice speaker having a conversation with a human.You ask the question the user choose the topic and let user answer.Provide the response only within 2 sentence"},
        {"role": "user",
         "content": f"Previous conversation:\n{chat_history}\n\nNew human question: {user_input}\nResponse:"}
    ]

    try:
        for chunk in AI71(AI71_API_KEY).chat.completions.create(
                model="tiiuae/falcon-180b-chat",
                messages=messages,
                stream=True,
        ):
            if chunk.choices[0].delta.content:
                msg += chunk.choices[0].delta.content
    except Exception as e:
        print(f"An error occurred: {e}")

    return msg


def get_first_youtube_video_link(query):
    url = f'https://www.youtube.com/results?search_query={query}'

    # Navigate to the URL
    driver.get(url)

    # Find the first video link
    try:
        # Find the first video element
        first_video_element = driver.find_element(By.XPATH, '//a[@id="video-title"]')
        video_link = first_video_element.get_attribute('href')
        print(video_link)
    finally:
        # Close the WebDriver
        driver.quit()



def content_translate(text):
    translated_content = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are the best teaching assistant."},
                {"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            translated_content += chunk.choices[0].delta.content
    return translated_content


def get_video_id(url):
    """
    Extract the video ID from a YouTube URL.
    """
    parsed_url = urlparse(url)
    if parsed_url.hostname == 'www.youtube.com' or parsed_url.hostname == 'youtube.com':
        video_id = parse_qs(parsed_url.query).get('v')
        if video_id:
            return video_id[0]
    elif parsed_url.hostname == 'youtu.be':
        return parsed_url.path[1:]
    return None


def extract_captions(video_url):
    """
    Extract captions from a YouTube video URL.
    """
    video_id = get_video_id(video_url)
    if not video_id:
        print("Invalid YouTube URL.")
        return

    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        formatter = JSONFormatter()
        formatted_transcript = formatter.format_transcript(transcript)

        # Save captions to a file
        with open(f'youtube_captions.json', 'w') as file:
            file.write(formatted_transcript)

        print("Captions have been extracted and saved as JSON.")

    except Exception as e:
        print(f"An error occurred: {e}")


def extract_text_from_json(filename):
    # Open and read the JSON file
    with open(filename, 'r') as file:
        data = json.load(file)

    # Extract and print the text fields
    texts = [entry['text'] for entry in data]
    return texts


def get_simplified_explanation(text):
    prompt = (
        f"The following is a transcript of a video: \n\n{text}\n\n"
        "Please provide a simplified explanation of the video for easy understanding."
    )

    response = ""
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": prompt},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            response += chunk.choices[0].delta.content

    return response


def summarise_text(url):
    extract_captions(url)
    texts = extract_text_from_json(r'youtube_captions.json')
    os.remove('youtube_captions.json')

    first_half = (get_simplified_explanation(texts[:len(texts) // 2]))
    second_half = (get_simplified_explanation(texts[len(texts) // 2:]))
    return (first_half + second_half)


def generate_speech_from_pdf(content):
    directory = 'speech'
    keep_file = 'nil.txt'
    
    # Check if the directory exists
    if os.path.isdir(directory):
        for filename in os.listdir(directory):
            file_path = os.path.join(directory, filename)
            
            # Check if the current file is not the one to keep and is a file
            if filename != keep_file and os.path.isfile(file_path):
                try:
                    os.remove(file_path)  # Delete the file
                    print(f"Deleted {file_path}")
                except Exception as e:
                    print(f"Error deleting {file_path}: {e}")
    else:
        print(f"Directory {directory} does not exist.")

    speech = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
            model="tiiuae/falcon-180b-chat",
            messages=[
                {"role": "system", "content": "You are a summarising assistant."},
                {"role": "user",
                 "content": f'''Summarise the given content for each chapter for 1 sentence.Content={content}'''},
            ],
            stream=True,
    ):
        if chunk.choices[0].delta.content:
            speech += chunk.choices[0].delta.content
    speech = speech[:-6].replace("###", '')
    chapters = speech.split('\n\n')
    pdf_audio(chapters[:4])
    return


def pdf_audio(chapters):
    for i in range(len(chapters)):
        tts = gTTS(text=chapters[i], lang='en', slow=False)
        tts.save(f'speech/chapter {i + 1}.mp3')
    return

def content_translate(text):


    translated_content = ''
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
        model="tiiuae/falcon-180b-chat",
        messages=[
            {"role": "system", "content": "You are the best teaching assistant."},
            {"role": "user", "content": f'''Translate the text to hindi. Text: {text}'''},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            translated_content += chunk.choices[0].delta.content
    return translated_content