Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -8,28 +8,31 @@ Original file is located at
|
|
| 8 |
|
| 9 |
##Creating app.py
|
| 10 |
|
| 11 |
-
"""
|
|
|
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
"""###
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
|
| 22 |
-
"""###Defining the prediction function"""
|
| 23 |
|
| 24 |
def generate_text(prompt):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
"""###Creating the Gradio interface
|
| 31 |
|
| 32 |
-
"""
|
| 33 |
|
| 34 |
api = gr.Interface(
|
| 35 |
fn=generate_text,
|
|
@@ -37,6 +40,7 @@ api = gr.Interface(
|
|
| 37 |
outputs=gr.Textbox(label="Generated Text"),
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
"""###Launching the API"""
|
| 41 |
|
| 42 |
api.launch()
|
|
|
|
| 8 |
|
| 9 |
##Creating app.py
|
| 10 |
|
| 11 |
+
"""
|
| 12 |
+
###Importing Dependencies
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
+
import requests
|
| 16 |
+
import json
|
| 17 |
+
import os
|
| 18 |
|
| 19 |
+
"""###Defining the prediction function that calls the model via API"""
|
| 20 |
|
| 21 |
+
API_URL = "https://api.yourmodelprovider.com/generate" # Replace with your API endpoint
|
| 22 |
+
HEADERS = {"Authorization": f"Bearer {os.getenv('API_KEY')}"} # Read the API key from an environment variable
|
|
|
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
def generate_text(prompt):
|
| 26 |
+
payload = {"prompt": prompt, "max_length": 100}
|
| 27 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 28 |
+
if response.status_code == 200:
|
| 29 |
+
generated_text = response.json().get("generated_text", "")
|
| 30 |
+
return generated_text
|
| 31 |
+
else:
|
| 32 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 33 |
|
|
|
|
| 34 |
|
| 35 |
+
"""###Creating the Gradio interface"""
|
| 36 |
|
| 37 |
api = gr.Interface(
|
| 38 |
fn=generate_text,
|
|
|
|
| 40 |
outputs=gr.Textbox(label="Generated Text"),
|
| 41 |
)
|
| 42 |
|
| 43 |
+
|
| 44 |
"""###Launching the API"""
|
| 45 |
|
| 46 |
api.launch()
|