IsmaelMousa commited on
Commit
6d2ae27
1 Parent(s): 48f2363

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +97 -5
main.py CHANGED
@@ -1,16 +1,109 @@
1
  from fastapi import FastAPI
 
2
  from transformers import pipeline
 
3
 
4
  app = FastAPI()
 
 
 
 
 
 
 
5
  pipe = pipeline(task="text-generation", model="gpt2-large", framework="pt")
6
 
7
 
8
- @app.get("/")
9
- def root():
10
  """
11
- Returns home page.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  """
13
- return {"message": "Hello Ismael"}
14
 
15
 
16
  @app.get("/generate")
@@ -21,5 +114,4 @@ def generate(text: str):
21
  can be found [here](<https://huggingface.co/openai-community/gpt2-large>).
22
  """
23
  output = pipe(text)
24
-
25
  return {"output": output[0]["generated_text"]}
 
1
  from fastapi import FastAPI
2
+ from fastapi.responses import HTMLResponse
3
  from transformers import pipeline
4
+ from fastapi.middleware.cors import CORSMiddleware
5
 
6
  app = FastAPI()
7
+
8
+ app.add_middleware(CORSMiddleware,
9
+ allow_origins=["*"],
10
+ allow_credentials=True,
11
+ allow_methods=["*"],
12
+ allow_headers=["*"])
13
+
14
  pipe = pipeline(task="text-generation", model="gpt2-large", framework="pt")
15
 
16
 
17
+ @app.get(path="/", response_class=HTMLResponse)
18
+ def get_ui():
19
  """
20
+ Returns the HTML page for the UI.
21
+ """
22
+ html_content = """
23
+ <!DOCTYPE html>
24
+ <html lang="en">
25
+ <head>
26
+ <meta charset="UTF-8">
27
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
28
+ <title>Text Generator</title>
29
+ <style>
30
+ body {
31
+ font-family: Arial, sans-serif;
32
+ display: flex;
33
+ justify-content: center;
34
+ align-items: center;
35
+ min-height: 100vh;
36
+ margin: 0;
37
+ padding: 20px;
38
+ background-color: #f5f5f5;
39
+ }
40
+ #container {
41
+ width: 100%;
42
+ max-width: 600px;
43
+ background: white;
44
+ padding: 20px;
45
+ border-radius: 8px;
46
+ box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
47
+ box-sizing: border-box;
48
+ }
49
+ #prompt, #output {
50
+ width: 100%;
51
+ padding: 10px;
52
+ margin-bottom: 20px;
53
+ border: 1px solid #ddd;
54
+ border-radius: 4px;
55
+ box-sizing: border-box;
56
+ }
57
+ #output {
58
+ min-height: 100px;
59
+ white-space: pre-wrap;
60
+ background-color: #f9f9f9;
61
+ padding: 15px;
62
+ border-radius: 4px;
63
+ }
64
+ </style>
65
+ </head>
66
+ <body>
67
+ <div id="container">
68
+ <h1>Text Generator</h1>
69
+ <textarea id="prompt" rows="4" placeholder="Enter your prompt here..."></textarea>
70
+ <div id="output"></div>
71
+ </div>
72
+
73
+ <script>
74
+ document.getElementById("prompt").addEventListener("keydown", async function(event) {
75
+ if (event.key === "Enter" && !event.shiftKey) {
76
+ event.preventDefault();
77
+ const prompt = this.value.trim();
78
+ if (!prompt) return;
79
+
80
+ const outputDiv = document.getElementById("output");
81
+ outputDiv.innerHTML = "Loading...";
82
+ this.disabled = true;
83
+
84
+ const response = await fetch(`/generate?text=${encodeURIComponent(prompt)}`);
85
+ const data = await response.json();
86
+ const text = data.output;
87
+ outputDiv.innerHTML = "";
88
+ let i = 0;
89
+
90
+ function printText() {
91
+ if (i < text.length) {
92
+ outputDiv.innerHTML += text.charAt(i);
93
+ i++;
94
+ setTimeout(printText, 50);
95
+ } else {
96
+ document.getElementById("prompt").disabled = false;
97
+ }
98
+ }
99
+ printText();
100
+ }
101
+ });
102
+ </script>
103
+ </body>
104
+ </html>
105
  """
106
+ return html_content
107
 
108
 
109
  @app.get("/generate")
 
114
  can be found [here](<https://huggingface.co/openai-community/gpt2-large>).
115
  """
116
  output = pipe(text)
 
117
  return {"output": output[0]["generated_text"]}