Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
| 10 |
+
|
| 11 |
+
# Function to scrape content from a URL
|
| 12 |
+
def scrape_content(url):
|
| 13 |
+
response = requests.get(url)
|
| 14 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 15 |
+
|
| 16 |
+
# Example of extracting title and body content - modify based on actual structure of the websites
|
| 17 |
+
title = soup.find('title').get_text()
|
| 18 |
+
paragraphs = soup.find_all('p')
|
| 19 |
+
content = '\n'.join([para.get_text() for para in paragraphs])
|
| 20 |
+
|
| 21 |
+
return title, content
|
| 22 |
+
|
| 23 |
+
# Function to create flashcards using DeepSeek API
|
| 24 |
+
def create_flashcards(content):
|
| 25 |
+
# Fetch the secret prompt from environment variables
|
| 26 |
+
prompt = os.getenv("FLASHCARD_PROMPT")
|
| 27 |
+
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 30 |
+
"Content-Type": "application/json"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
data = {
|
| 34 |
+
"model": "deepseek-chat", # Replace with the appropriate DeepSeek model
|
| 35 |
+
"messages": [
|
| 36 |
+
{"role": "system", "content": prompt},
|
| 37 |
+
{"role": "user", "content": content}
|
| 38 |
+
],
|
| 39 |
+
"max_tokens": 500,
|
| 40 |
+
"temperature": 0.7
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
response = requests.post(
|
| 44 |
+
"https://api.deepseek.com/v1/chat/completions", # Replace with the correct DeepSeek API endpoint
|
| 45 |
+
headers=headers,
|
| 46 |
+
json=data
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
response_json = response.json()
|
| 50 |
+
flashcards = response_json["choices"][0]["message"]["content"].strip()
|
| 51 |
+
return flashcards
|
| 52 |
+
|
| 53 |
+
# Function to process a single URL and generate flashcards
|
| 54 |
+
def process_url(url):
|
| 55 |
+
if not url:
|
| 56 |
+
return "No URL provided."
|
| 57 |
+
|
| 58 |
+
title, content = scrape_content(url)
|
| 59 |
+
flashcards = create_flashcards(content)
|
| 60 |
+
|
| 61 |
+
# Save flashcards as a text file
|
| 62 |
+
file_path = "flashcards.txt"
|
| 63 |
+
with open(file_path, "w") as file:
|
| 64 |
+
file.write(f"Title: {title}\n\nFlashcards:\n{flashcards}")
|
| 65 |
+
|
| 66 |
+
return flashcards, file_path
|
| 67 |
+
|
| 68 |
+
# Gradio interface
|
| 69 |
+
def interface_fn(url):
|
| 70 |
+
flashcards, file_path = process_url(url)
|
| 71 |
+
return flashcards, file_path
|
| 72 |
+
|
| 73 |
+
iface = gr.Interface(
|
| 74 |
+
fn=interface_fn,
|
| 75 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter URL here..."),
|
| 76 |
+
outputs=["text", gr.File(label="Download Flashcards")],
|
| 77 |
+
title="The Federal Flash Card Generator",
|
| 78 |
+
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."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Launch the interface
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
iface.launch(share=False)
|