File size: 3,532 Bytes
b7709ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
import streamlit as st
import base64
import pytesseract
from PIL import Image
from PyPDF2 import PdfReader
from openai import OpenAI
import os

# Retrieve the OpenAI API Key from the secrets
openai_api_key = st.secrets["OPENAI_API_KEY"]
client = OpenAI(api_key=openai_api_key)

# File upload
st.title("Functional Specification Document Processor")
uploaded_file = st.file_uploader("Upload a Functional Spec Document (PDF, TXT, JPG)", type=["pdf", "txt", "jpg", "jpeg"])

# Extract text from different file types
def extract_text(file):
    text = ""
    if file.type == "application/pdf":
        reader = PdfReader(file)
        for page in reader.pages:
            if page and page.extract_text():
                text += page.extract_text()
    elif file.type == "text/plain":
        text = str(file.read(), "utf-8")
    elif file.type in ["image/jpeg", "image/jpg"]:
        image = Image.open(file)
        text = pytesseract.image_to_string(image)
    return text

# Base64 encoding
def encode_base64(text):
    return base64.b64encode(text.encode("utf-8")).decode("utf-8")

# Step 1: Identify epics, features, and user stories in a breadcrumb structure
def identify_structure(processed_text):
    prompt = f"""
    You are a product owner working from a functional specification document. 
    Analyze the document and identify the high-level epics, features, and detailed user stories.
    Structure them in a clear breadcrumb format (Epic > Feature > User Stories). Include titles and short descriptions.
    
    Document:
    {processed_text}
    """
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=2000
    )
    return response.choices[0].message.content.strip()

# Step 2: Generate detailed outcomes for a specific user story
def generate_user_story_details(user_story):
    prompt = f"""
    You are a product owner. Based on the following user story, create a detailed functional breakdown.
    Include:
    - User story description
    - Acceptance criteria
    - Detailed steps and requirements
    
    User Story:
    {user_story}
    """
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=1500
    )
    return response.choices[0].message.content.strip()

if uploaded_file:
    with st.spinner("Processing document..."):
        extracted_text = extract_text(uploaded_file)
        if extracted_text:
            st.subheader("Identified Epics, Features, and User Stories")
            structure = identify_structure(extracted_text)
            st.text_area("Structure", structure, height=300)

            user_story = st.text_input("Copy and paste a user story from the structure above to generate details:")
            if user_story:
                details = generate_user_story_details(user_story)
                st.subheader("Detailed User Story Breakdown")
                st.text_area("Details", details, height=400)

                if st.button("Download as TXT"):
                    st.download_button(label="Download", data=details, file_name="user_story_details.txt", mime="text/plain")
        else:
            st.error("Failed to extract text from the document. Please try again with a clearer document.")