File size: 2,708 Bytes
024babc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c7de98
024babc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup
import gradio as gr
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")

# Function to scrape content from a URL
def scrape_content(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')

    # Example of extracting title and body content - modify based on actual structure of the websites
    title = soup.find('title').get_text()
    paragraphs = soup.find_all('p')
    content = '\n'.join([para.get_text() for para in paragraphs])

    return title, content

# Function to create flashcards using DeepSeek API
def create_flashcards(content):
    # Fetch the secret prompt from environment variables
    prompt = os.getenv("FLASHCARD_PROMPT")

    headers = {
        "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "deepseek-chat",  # Replace with the appropriate DeepSeek model the new one is DeepSeek-V3 but madness so wait for that to subside!
        "messages": [
            {"role": "system", "content": prompt},
            {"role": "user", "content": content}
        ],
        "max_tokens": 500,
        "temperature": 0.7
    }

    response = requests.post(
        "https://api.deepseek.com/v1/chat/completions",  # Replace with the correct DeepSeek API endpoint
        headers=headers,
        json=data
    )

    response_json = response.json()
    flashcards = response_json["choices"][0]["message"]["content"].strip()
    return flashcards

# Function to process a single URL and generate flashcards
def process_url(url):
    if not url:
        return "No URL provided."

    title, content = scrape_content(url)
    flashcards = create_flashcards(content)

    # Save flashcards as a text file
    file_path = "flashcards.txt"
    with open(file_path, "w") as file:
        file.write(f"Title: {title}\n\nFlashcards:\n{flashcards}")

    return flashcards, file_path

# Gradio interface
def interface_fn(url):
    flashcards, file_path = process_url(url)
    return flashcards, file_path

iface = gr.Interface(
    fn=interface_fn,
    inputs=gr.Textbox(lines=2, placeholder="Enter URL here..."),
    outputs=["text", gr.File(label="Download Flashcards")],
    title="The Federal Flash Card Generator",
    description="An application that helps students prepare for competitive exams like the UPSC, SSC, CLAT, MBA, etc. Enter a News Site URL to generate flashcards for exam preparation. Option to download the response as a text file."
)

# Launch the interface
if __name__ == "__main__":
    iface.launch(share=False)