Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spacy
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load spaCy model for prompt parsing
|
| 9 |
+
nlp = spacy.load("en_core_web_sm")
|
| 10 |
+
|
| 11 |
+
# Load text generation model
|
| 12 |
+
text_generator = pipeline('text-generation', model='gpt2')
|
| 13 |
+
|
| 14 |
+
# Load dataset
|
| 15 |
+
dataset_path = "public_service_data.csv" # Ensure this file is uploaded to your Hugging Face Space
|
| 16 |
+
if os.path.exists(dataset_path):
|
| 17 |
+
data_df = pd.read_csv(dataset_path)
|
| 18 |
+
else:
|
| 19 |
+
data_df = pd.DataFrame({"growth_rate": [0]}) # Default structure if the file isn't present
|
| 20 |
+
|
| 21 |
+
def generate_project_document(prompt, document_type):
|
| 22 |
+
# Parse prompt using spaCy
|
| 23 |
+
doc = nlp(prompt)
|
| 24 |
+
|
| 25 |
+
# Identify key elements and required sections
|
| 26 |
+
industry = next((ent.text for ent in doc.ents if ent.label_ == 'ORG'), "the relevant industry")
|
| 27 |
+
product = next((ent.text for ent in doc.ents if ent.label_ == 'PRODUCT'), "the specified product")
|
| 28 |
+
|
| 29 |
+
if document_type == 'Project Proposal':
|
| 30 |
+
sections = ['Introduction', 'Objectives', 'Methodology', 'Expected Outcomes']
|
| 31 |
+
elif document_type == 'Report':
|
| 32 |
+
sections = ['Executive Summary', 'Findings', 'Analysis', 'Conclusion']
|
| 33 |
+
elif document_type == 'Summary':
|
| 34 |
+
sections = ['Overview', 'Key Points', 'Conclusion']
|
| 35 |
+
else:
|
| 36 |
+
return None, "Invalid document type"
|
| 37 |
+
|
| 38 |
+
# Generate content for each section
|
| 39 |
+
content = defaultdict(str)
|
| 40 |
+
for section in sections:
|
| 41 |
+
prompt_text = f"Generate {section.lower()} for a {industry} project on {product}."
|
| 42 |
+
content[section] = text_generator(prompt_text, max_length=200, num_return_sequences=1)[0]['generated_text']
|
| 43 |
+
|
| 44 |
+
# Enrich content with data
|
| 45 |
+
if document_type == 'Project Proposal':
|
| 46 |
+
growth_rate = data_df['growth_rate'].mean() if not data_df.empty else 0
|
| 47 |
+
content['Introduction'] += f"\nThe {industry} industry has witnessed significant advancements in {product} technology. According to the latest industry data, the market for {product} is expected to grow by {growth_rate:.2f}% annually."
|
| 48 |
+
|
| 49 |
+
# Compile document as string to return
|
| 50 |
+
document_content = "\n\n".join([f"{section}\n{text}" for section, text in content.items()])
|
| 51 |
+
return document_content
|
| 52 |
+
|
| 53 |
+
# Gradio interface
|
| 54 |
+
def app():
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
prompt = gr.Textbox(label="Document Prompt")
|
| 59 |
+
document_type = gr.Dropdown(label="Document Type", choices=["Project Proposal", "Report", "Summary"])
|
| 60 |
+
submit = gr.Button("Generate Document")
|
| 61 |
+
with gr.Column():
|
| 62 |
+
output = gr.Textbox(label="Generated Document Content", lines=15)
|
| 63 |
+
|
| 64 |
+
submit.click(generate_project_document, inputs=[prompt, document_type], outputs=output)
|
| 65 |
+
|
| 66 |
+
demo.launch()
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
app()
|