|
| 1 | +from fastapi import APIRouter |
| 2 | +import openai |
| 3 | +import requests |
| 4 | +from bs4 import BeautifulSoup |
| 5 | +import os,shutil |
| 6 | +from urllib.parse import urlparse, urljoin |
| 7 | + |
| 8 | +openai.api_key = "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" |
| 9 | +# Create an instance of APIRouter |
| 10 | +router = APIRouter() |
| 11 | + |
| 12 | + |
| 13 | +openai.api_key = "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" |
| 14 | + |
| 15 | +# Process each paragraph and search for related images |
| 16 | +def process_paragraphs(paragraphs): |
| 17 | + # Create the "images" directory if it doesn't exist |
| 18 | + if not os.path.exists("images"): |
| 19 | + os.makedirs("images") |
| 20 | + for query in paragraphs: |
| 21 | + image_url = search_image(query) |
| 22 | + if image_url: |
| 23 | + # Download the image and save it locally |
| 24 | + response = requests.get(image_url) |
| 25 | + if response.status_code == 200: |
| 26 | + with open(f"images/image_{query}.jpg", 'wb') as f: |
| 27 | + f.write(response.content) |
| 28 | + print(f"Image_{query}.jpg saved.") |
| 29 | + |
| 30 | +def search_image(query): |
| 31 | + url = f"https://www.google.com/search?q={query}&tbm=isch&tbs=isz:l" |
| 32 | + headers = { |
| 33 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" |
| 34 | + } |
| 35 | + response = requests.get(url, headers=headers) |
| 36 | + soup = BeautifulSoup(response.text, 'html.parser') |
| 37 | + image_elements = soup.find_all('img') |
| 38 | + image_url = None |
| 39 | + if len(image_elements) > 1: |
| 40 | + image_url = image_elements[1].get('src') |
| 41 | + if image_url: |
| 42 | + parsed_url = urlparse(image_url) |
| 43 | + if not parsed_url.scheme: |
| 44 | + base_url = response.url |
| 45 | + image_url = urljoin(base_url, image_url) |
| 46 | + return image_url |
| 47 | + |
| 48 | + |
| 49 | +def extract_text_from_file(file_path): |
| 50 | + try: |
| 51 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 52 | + text = file.read() |
| 53 | + return text |
| 54 | + except FileNotFoundError: |
| 55 | + print(f"File '{file_path}' not found.") |
| 56 | + return None |
| 57 | + |
| 58 | +def extract_important_topics(questions): |
| 59 | + text = questions |
| 60 | + response = openai.ChatCompletion.create( |
| 61 | + model="gpt-3.5-turbo", |
| 62 | + messages=[ |
| 63 | + { |
| 64 | + "role": "user", |
| 65 | + "content": f"You are a teacher, who will take a Topic to a student. I will provide you with a basic summary of the student's lecture note, which may be a bit incomplete, but you have to use it and interact with the student and make sure they understand it fully. Basically, you have to narrate (everything should be text):\n\n{text}\n\n" |
| 66 | + } |
| 67 | + ] |
| 68 | + ) |
| 69 | + |
| 70 | + important_topics = response.choices[0].message.content |
| 71 | + print(important_topics) |
| 72 | + return important_topics |
| 73 | + |
| 74 | + |
| 75 | +def extract_image_name(questions): |
| 76 | + text = questions |
| 77 | + response = openai.ChatCompletion.create( |
| 78 | + model="gpt-3.5-turbo", |
| 79 | + messages=[ |
| 80 | + { |
| 81 | + "role": "user", |
| 82 | + "content": f"create a word for each paragraph as a list\n\n{text}\n\n" |
| 83 | + } |
| 84 | + ] |
| 85 | + ) |
| 86 | + |
| 87 | + important_topics = response.choices[0].message.content |
| 88 | + print(important_topics) |
| 89 | + return important_topics |
0 commit comments