baqarhussain commited on
Commit
ebf461d
·
verified ·
1 Parent(s): dda4749

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -43
app.py CHANGED
@@ -1,58 +1,66 @@
1
- # Import necessary libraries
2
- import streamlit as st
3
  from docx import Document
4
  import matplotlib.pyplot as plt
5
  import io
 
6
 
7
- # Function to read data from the Word file
8
- def extract_data_from_word(file):
9
  document = Document(file)
10
- text = []
 
11
  for paragraph in document.paragraphs:
12
- text.append(paragraph.text)
13
- return " ".join(text)
14
-
15
- # Function to calculate performance metric (example)
16
- def calculate_performance(data):
17
- # Dummy calculation: length of the text as a percentage of 1000
18
- return min(len(data) / 1000 * 100, 100) # Cap at 100%
19
-
20
- # Function to create a performance meter using matplotlib
21
- def create_performance_meter(value):
22
- fig, ax = plt.subplots(figsize=(6, 3))
23
- ax.set_xlim(0, 100)
24
- ax.set_ylim(0, 1)
25
- ax.barh(0.5, value, color="green", height=0.5, label=f"Performance: {value:.2f}%")
26
- ax.barh(0.5, 100 - value, left=value, color="red", height=0.5)
27
- ax.set_xticks(range(0, 101, 10))
28
- ax.set_yticks([])
29
- ax.set_title("Performance Meter", fontsize=16)
30
- ax.legend(loc="upper right")
 
 
 
 
 
 
 
 
 
 
 
 
31
  plt.tight_layout()
32
 
33
- # Save plot to a BytesIO buffer
34
  buffer = io.BytesIO()
35
  plt.savefig(buffer, format="png")
36
  buffer.seek(0)
37
  return buffer
38
 
39
- # Streamlit App
40
- st.title("Dashboard Meter Application")
41
- st.write("Upload a Word file to analyze and visualize the performance.")
42
-
43
- # File upload section
44
- uploaded_file = st.file_uploader("Upload a Word file (.docx)", type="docx")
45
- if uploaded_file:
46
- # Step 1: Extract text data
47
- data = extract_data_from_word(uploaded_file)
48
- st.write("Extracted Text:")
49
- st.text_area("Data Preview", value=data, height=200)
50
-
51
- # Step 2: Calculate performance metric
52
- performance = calculate_performance(data)
53
 
54
- # Step 3: Display the performance meter
55
- st.write(f"Performance Score: {performance:.2f}%")
56
- meter_image = create_performance_meter(performance)
57
- st.image(meter_image, caption="Performance Meter")
58
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
 
2
  from docx import Document
3
  import matplotlib.pyplot as plt
4
  import io
5
+ from collections import Counter
6
 
7
+ # Function to extract Name and Status from the Word file
8
+ def extract_name_status(file):
9
  document = Document(file)
10
+ name_status_list = []
11
+
12
  for paragraph in document.paragraphs:
13
+ if "Name:" in paragraph.text and "Status:" in paragraph.text:
14
+ parts = paragraph.text.split(",")
15
+ name_part = next((part for part in parts if "Name:" in part), None)
16
+ status_part = next((part for part in parts if "Status:" in part), None)
17
+
18
+ if name_part and status_part:
19
+ name = name_part.split(":")[1].strip()
20
+ status = status_part.split(":")[1].strip()
21
+ name_status_list.append((name, status))
22
+
23
+ return name_status_list
24
+
25
+ # Function to create a bar chart for Name vs Status
26
+ def create_graph(file):
27
+ data = extract_name_status(file)
28
+
29
+ if not data:
30
+ return "No valid Name-Status data found in the file."
31
+
32
+ names = [item[0] for item in data]
33
+ statuses = [item[1] for item in data]
34
+
35
+ # Count statuses
36
+ status_counts = Counter(statuses)
37
+
38
+ # Plotting the graph
39
+ fig, ax = plt.subplots(figsize=(8, 6))
40
+ ax.bar(status_counts.keys(), status_counts.values(), color="skyblue")
41
+ ax.set_xlabel("Status", fontsize=14)
42
+ ax.set_ylabel("Count", fontsize=14)
43
+ ax.set_title("Graph: Name vs Status", fontsize=16)
44
  plt.tight_layout()
45
 
46
+ # Save the plot to a buffer
47
  buffer = io.BytesIO()
48
  plt.savefig(buffer, format="png")
49
  buffer.seek(0)
50
  return buffer
51
 
52
+ # Gradio interface
53
+ def interface(file):
54
+ return create_graph(file)
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ # Launch the Gradio app
57
+ inputs = gr.inputs.File(label="Upload Word File (.docx)")
58
+ outputs = gr.outputs.Image(type="file", label="Graph: Name vs Status")
 
59
 
60
+ gr.Interface(
61
+ fn=interface,
62
+ inputs=inputs,
63
+ outputs=outputs,
64
+ title="Name vs Status Graph",
65
+ description="Upload a Word file containing 'Name: <Name>, Status: <Status>' data to generate a graph."
66
+ ).launch()