Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from openai import OpenAI
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Set up OpenAI API key
|
10 |
+
api_key = os.getenv("OPENAI_API_KEY") # Ensure you have the OpenAI API key in .env
|
11 |
+
client = OpenAI(api_key=api_key)
|
12 |
+
|
13 |
+
# Function to query OpenAI for content summary
|
14 |
+
def generate_summary(text, model="gpt-4o-mini"):
|
15 |
+
try:
|
16 |
+
# Call OpenAI's API for text summarization
|
17 |
+
response = client.chat.completions.create(
|
18 |
+
model=model,
|
19 |
+
messages=[
|
20 |
+
{"role": "system", "content": "You are a helpful assistant. Please summarize the following content."},
|
21 |
+
{"role": "user", "content": text}
|
22 |
+
]
|
23 |
+
)
|
24 |
+
return response.choices[0].message.content # Direct access to the message content
|
25 |
+
except Exception as e:
|
26 |
+
return f"Error: {e}"
|
27 |
+
|
28 |
+
# Gradio app layout
|
29 |
+
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("# Content Summarizer")
|
31 |
+
gr.Markdown("### Upload a text file or paste text, and the app will generate a summary for you.")
|
32 |
+
|
33 |
+
# File upload
|
34 |
+
file_upload = gr.File(label="Upload Text File", type="filepath")
|
35 |
+
|
36 |
+
# Text input area
|
37 |
+
text_input = gr.Textbox(label="Or Paste Your Text Here", lines=100)
|
38 |
+
|
39 |
+
# Output area for the summary
|
40 |
+
summary_output = gr.Textbox(label="Summary", lines=5)
|
41 |
+
|
42 |
+
# Button to generate the summary
|
43 |
+
generate_button = gr.Button("Generate Summary")
|
44 |
+
|
45 |
+
# Logic to handle file input or text input and generate the summary
|
46 |
+
def handle_input(file, text):
|
47 |
+
if file is not None:
|
48 |
+
# Read the uploaded file
|
49 |
+
text = file.name.read().decode("utf-8")
|
50 |
+
|
51 |
+
if text.strip():
|
52 |
+
# Generate summary
|
53 |
+
summary = generate_summary(text)
|
54 |
+
return summary
|
55 |
+
else:
|
56 |
+
return "Please enter some text to summarize."
|
57 |
+
|
58 |
+
generate_button.click(fn=handle_input, inputs=[file_upload, text_input], outputs=summary_output)
|
59 |
+
|
60 |
+
# Launch the Gradio app
|
61 |
+
demo.launch()
|