ariansyahdedy commited on
Commit
f10d8ff
·
0 Parent(s):

Initial Commit

Browse files
Files changed (8) hide show
  1. .gitignore +7 -0
  2. Dockerfile +11 -0
  3. README.md +0 -0
  4. app/__init__.py +0 -0
  5. app/main.py +139 -0
  6. app/utils.py +0 -0
  7. huggingface.yaml +3 -0
  8. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ venv/
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ *.pyd
6
+ .env
7
+ .DS_Store
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY app /app
10
+
11
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
File without changes
app/__init__.py ADDED
File without changes
app/main.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import StreamingResponse
3
+ import asyncio
4
+ import json
5
+ import io
6
+ import base64
7
+ import matplotlib.pyplot as plt
8
+ import pandas as pd
9
+
10
+ app = FastAPI()
11
+
12
+ def build_text_stream():
13
+ """
14
+ Create the full text stream for the text-only endpoint.
15
+ The text is broken into words, with each word sent as a separate event.
16
+ """
17
+ # Full text as provided (simulate with the complete sample, add newlines for clarity)
18
+ text = (
19
+ "To be an outstanding front-end engineer, you'll need a combination of technical skills, "
20
+ "soft skills, and a passion for continuous learning. Here are the key areas to focus on: "
21
+ "**Technical Skills:** 1. **HTML/CSS**: A solid understanding of HTML5, CSS3, and their respective best practices "
22
+ "is essential for building robust and maintainable front-end applications. 2. **JavaScript**: Proficiency in JavaScript, "
23
+ "including its various frameworks and libraries such as React, Angular, or Vue.js, is crucial for creating dynamic and interactive user interfaces. "
24
+ "3. **Front-end frameworks and libraries**: Familiarity with popular front-end frameworks and libraries like Bootstrap, Material-UI, or Tailwind CSS "
25
+ "can help you build responsive and visually appealing interfaces. 4. **State management**: Understanding state management techniques, such as Redux or MobX, "
26
+ "is essential for managing complex application state and ensuring a seamless user experience. 5. **Performance optimization**: Knowledge of performance optimization techniques, "
27
+ "including code splitting, lazy loading, and caching, is vital for delivering fast and efficient applications. 6. **Accessibility**: Understanding web accessibility guidelines and "
28
+ "implementing them in your code is crucial for ensuring that your application is usable by everyone, regardless of abilities. 7. **Version control**: Familiarity with version control systems like Git "
29
+ "is essential for managing code changes, collaborating with team members, and maintaining a clean and organized codebase. "
30
+ "**Soft Skills:** 1. **Communication**: Effective communication is critical for collaborating with designers, back-end engineers, and other stakeholders to ensure that everyone is on the same page. "
31
+ "2. **Problem-solving**: Strong problem-solving skills are essential for debugging complex issues, identifying root causes, and implementing effective solutions. "
32
+ "3. **Adaptability**: The ability to adapt to changing requirements, new technologies, and evolving project needs is crucial for staying ahead of the curve and delivering high-quality results. "
33
+ "4. **Time management**: Effective time management is essential for meeting deadlines, prioritizing tasks, and delivering projects on time. "
34
+ "5. **Continuous learning**: A passion for continuous learning is vital for staying up-to-date with the latest front-end technologies, frameworks, and best practices. "
35
+ "**Best Practices:** 1. **Write clean and maintainable code**: Follow coding standards, use meaningful variable names, and keep your code organized and modular. "
36
+ "2. **Use version control**: Use Git or other version control systems to manage code changes, collaborate with team members, and maintain a clean and organized codebase. "
37
+ "3. **Test and debug**: Write unit tests, integration tests, and end-to-end tests to ensure that your code is working as expected and catch bugs early. "
38
+ "4. **Follow accessibility guidelines**: Implement web accessibility guidelines to ensure that your application is usable by everyone, regardless of abilities. "
39
+ "5. **Stay up-to-date with industry trends**: Participate in online communities, attend conferences, and read industry blogs to stay informed about the latest front-end technologies and best practices. "
40
+ "Examples of Outstanding Front-end Engineers: * **Linus Borg**: A renowned front-end engineer and speaker, known for his expertise in React, Redux, and performance optimization. "
41
+ "* **Addy Osmani**: A Google developer advocate and front-end engineer, recognized for his work on web performance, accessibility, and modern web development. "
42
+ "* **Max Stoiber**: A front-end engineer and speaker, known for his expertise in React, Redux, and state management. "
43
+ "By focusing on these technical skills, soft skills, and best practices, you can become an outstanding front-end engineer and deliver high-quality applications that meet the needs of your users."
44
+ )
45
+ # Break the text into individual words and newlines (if desired, you can also add empty lines as in your sample)
46
+ return text.split(" ")
47
+
48
+ async def text_stream_generator():
49
+ """
50
+ Generator that streams the complete text word by word.
51
+ """
52
+ words = build_text_stream()
53
+ for word in words:
54
+ yield f"data: {word}\n\n"
55
+ await asyncio.sleep(0.2) # Adjust the delay as needed
56
+
57
+ @app.post("/v1/test_text")
58
+ async def test_text():
59
+ return StreamingResponse(text_stream_generator(), media_type="text/event-stream")
60
+
61
+ def generate_chart():
62
+ """
63
+ Generate a Base64-encoded bar chart.
64
+ """
65
+ data = {
66
+ "territory": ["EMEA", "NA", "APAC", "Japan"],
67
+ "total_sales": [4979272.41, 3852061.39, 746121.83, 455173.22]
68
+ }
69
+ df = pd.DataFrame(data)
70
+
71
+ plt.figure(figsize=(6, 4))
72
+ plt.bar(df["territory"], df["total_sales"], color=["blue", "green", "red", "orange"])
73
+ plt.xlabel("Market Territory")
74
+ plt.ylabel("Total Sales")
75
+ plt.title("Total Sales by Market Territory")
76
+
77
+ img_buffer = io.BytesIO()
78
+ plt.savefig(img_buffer, format="png")
79
+ plt.close()
80
+
81
+ img_buffer.seek(0)
82
+ return base64.b64encode(img_buffer.getvalue()).decode("utf-8")
83
+
84
+ async def chart_text_generator():
85
+ """
86
+ Generator that streams both a Base64-encoded chart and text word by word.
87
+ """
88
+ # Prepare chart data with extra configuration if needed
89
+ chart_payload = {
90
+ "chartType": "BarChart",
91
+ "data": [
92
+ {"territory": "EMEA", "total_sales": 4979272.41},
93
+ {"territory": "NA", "total_sales": 3852061.39},
94
+ {"territory": "APAC", "total_sales": 746121.83},
95
+ {"territory": "Japan", "total_sales": 455173.22}
96
+ ],
97
+ "config": {
98
+ "xKey": "territory",
99
+ "xAxisLabel": "Market Territory",
100
+ "yAxisLabel": "Total Sales",
101
+ "yAxis": {
102
+ "label": {"value": "Total Sales", "angle": -90, "position": "insideLeft", "offset": -60, "dy": 10},
103
+ "tick": {"fontSize": 12},
104
+ "padding": {"top": 10, "bottom": 10}
105
+ },
106
+ "series": [
107
+ {"dataKey": "total_sales", "color": "#8884d8", "name": "Total Sales"}
108
+ ],
109
+ "dataPointCount": 4,
110
+ "tooltipFormatterType": "usd",
111
+ "tooltipLabelFormatterType": None,
112
+ "yAxisTickFormatterType": "shortNumber",
113
+ "margin": {"top": 20, "right": 30, "left": 60, "bottom": 60}
114
+ },
115
+ "chartTitle": "Total Sales by Market Territory",
116
+ "chartImage": generate_chart()
117
+ }
118
+
119
+ # First send the complete chart JSON as one event
120
+ yield f"data: {json.dumps(chart_payload)}\n\n"
121
+ await asyncio.sleep(1) # Brief pause before streaming the text
122
+
123
+ # Now stream the text details word-by-word
124
+ full_text = (
125
+ "The current sales data for different regions is as follows: "
126
+ "EMEA (Europe, Middle East, and Africa): $4,979,272.41, "
127
+ "NA (North America): $3,852,061.39, "
128
+ "APAC (Asia Pacific): $746,121.83, "
129
+ "and Japan: $455,173.22. "
130
+ "These figures represent the total sales for each region, providing a snapshot of the current sales performance across different geographic areas."
131
+ )
132
+ words = full_text.split(" ")
133
+ for word in words:
134
+ yield f"data: {word}\n\n"
135
+ await asyncio.sleep(0.2)
136
+
137
+ @app.post("/v1/test_chart")
138
+ async def test_chart():
139
+ return StreamingResponse(chart_text_generator(), media_type="text/event-stream")
app/utils.py ADDED
File without changes
huggingface.yaml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ sdk: docker
2
+ repository: frontend_test
3
+ port: 7860
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ matplotlib
4
+ pandas
5
+ aiohttp