Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import openai
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
# Function to scrape content from a URL
|
| 13 |
+
def scrape_content(url):
|
| 14 |
+
response = requests.get(url)
|
| 15 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 16 |
+
|
| 17 |
+
# Example of extracting title and body content - modify based on actual structure of the websites
|
| 18 |
+
title = soup.find('title').get_text()
|
| 19 |
+
paragraphs = soup.find_all('p')
|
| 20 |
+
content = '\n'.join([para.get_text() for para in paragraphs])
|
| 21 |
+
|
| 22 |
+
return title, content
|
| 23 |
+
|
| 24 |
+
# Function to create flashcards using OpenAI
|
| 25 |
+
def create_flashcards(content):
|
| 26 |
+
prompt = f"Create flashcards for UPSC exam preparation from the following content:\n\n{content}\n\n"
|
| 27 |
+
|
| 28 |
+
response = openai.ChatCompletion.create(
|
| 29 |
+
model="gpt-3.5-turbo",
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": "You are a helpful assistant for UPSC exam preparation."},
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
max_tokens=500,
|
| 35 |
+
temperature=0.7
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
flashcards = response.choices[0].message['content'].strip()
|
| 39 |
+
return flashcards
|
| 40 |
+
|
| 41 |
+
# Function to process a single URL and generate flashcards
|
| 42 |
+
def process_url(url):
|
| 43 |
+
if not url:
|
| 44 |
+
return "No URL provided."
|
| 45 |
+
|
| 46 |
+
title, content = scrape_content(url)
|
| 47 |
+
flashcards = create_flashcards(content)
|
| 48 |
+
return f"Title: {title}\n\nFlashcards:\n{flashcards}"
|
| 49 |
+
|
| 50 |
+
# Gradio interface
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=process_url,
|
| 53 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter URL here..."),
|
| 54 |
+
outputs="text",
|
| 55 |
+
title="UPSC Flashcard Generator",
|
| 56 |
+
description="Enter a URL to generate flashcards for UPSC preparation."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Launch the interface
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch(share=True)
|