Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- .env +2 -0
- Dockerfile +14 -0
- app.py +62 -0
- requirements.txt +7 -0
.env
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DEEPGRAM_API_KEY='43044985b4f715eda085e2b5e974f14edb2abf81'
|
| 2 |
+
OPENAI_API_KEY='9b3fd60a04ff4517be88e57bb91f205f'
|
Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python 3.10.9 image
|
| 2 |
+
FROM python:3.10.9
|
| 3 |
+
|
| 4 |
+
# Copy the current directory contents into the container at .
|
| 5 |
+
COPY . .
|
| 6 |
+
|
| 7 |
+
# Set the working directory to /
|
| 8 |
+
WORKDIR /
|
| 9 |
+
|
| 10 |
+
# Install requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
| 12 |
+
|
| 13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
| 14 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
from langchain.chains import LLMChain
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
import openai
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Load environment variables from .env file
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Configure OpenAI for Azure
|
| 15 |
+
openai.api_type = "azure"
|
| 16 |
+
openai.api_base = "https://amplifai-openai.openai.azure.com/"
|
| 17 |
+
openai.api_version = "2023-07-01-preview"
|
| 18 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
|
| 20 |
+
app = FastAPI()
|
| 21 |
+
|
| 22 |
+
class Prompt(BaseModel):
|
| 23 |
+
text: str
|
| 24 |
+
|
| 25 |
+
class GenerateResponse(BaseModel):
|
| 26 |
+
text: str
|
| 27 |
+
|
| 28 |
+
def generate_text(prompt: str):
|
| 29 |
+
if prompt == "":
|
| 30 |
+
return {"detail": "Please provide a prompt."}
|
| 31 |
+
else:
|
| 32 |
+
prompt_template = PromptTemplate(template=prompt, input_variables=['Prompt'])
|
| 33 |
+
llm = OpenAI(api_key=openai.api_key)
|
| 34 |
+
llmchain = LLMChain(
|
| 35 |
+
prompt=prompt_template,
|
| 36 |
+
llm=llm
|
| 37 |
+
)
|
| 38 |
+
llm_response = llmchain.run(
|
| 39 |
+
{"Prompt": prompt},
|
| 40 |
+
temperature=0.7,
|
| 41 |
+
max_tokens=750,
|
| 42 |
+
top_p=1.0,
|
| 43 |
+
frequency_penalty=0.0,
|
| 44 |
+
presence_penalty=0.0
|
| 45 |
+
)
|
| 46 |
+
return GenerateResponse(text=llm_response)
|
| 47 |
+
|
| 48 |
+
app.add_middleware(
|
| 49 |
+
CORSMiddleware,
|
| 50 |
+
allow_origins=["*"],
|
| 51 |
+
allow_credentials=True,
|
| 52 |
+
allow_methods=["*"],
|
| 53 |
+
allow_headers=["*"],
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
@app.get("/", tags=["Home"])
|
| 57 |
+
def api_home():
|
| 58 |
+
return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
|
| 59 |
+
|
| 60 |
+
@app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse)
|
| 61 |
+
def inference(input_prompt: Prompt):
|
| 62 |
+
return generate_text(prompt=input_prompt.text)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.99.1
|
| 2 |
+
uvicorn
|
| 3 |
+
requests
|
| 4 |
+
langchain
|
| 5 |
+
openai==0.28.0
|
| 6 |
+
pillow==10.2.0
|
| 7 |
+
pydantic==2.6.1
|