Update main.py
Browse files
main.py
CHANGED
|
@@ -94,53 +94,3 @@ def extract_fields(input_data: ExtractInput):
|
|
| 94 |
raise HTTPException(status_code=500, detail=str(e))
|
| 95 |
|
| 96 |
|
| 97 |
-
# Load environment variables from the .env file
|
| 98 |
-
load_dotenv()
|
| 99 |
-
|
| 100 |
-
api_key = os.getenv("EXTRACTSUMMARY_API_KEY")
|
| 101 |
-
|
| 102 |
-
if not api_key:
|
| 103 |
-
raise ValueError("ExtractSummary API Key not found in environment variables!")
|
| 104 |
-
|
| 105 |
-
client = Groq(api_key=api_key)
|
| 106 |
-
|
| 107 |
-
app = FastAPI()
|
| 108 |
-
|
| 109 |
-
# Your route handling
|
| 110 |
-
@app.get("/")
|
| 111 |
-
def read_root():
|
| 112 |
-
return {"message": "Hello, FastAPI!"}
|
| 113 |
-
|
| 114 |
-
class TextInput(BaseModel):
|
| 115 |
-
text: str
|
| 116 |
-
max_words: int = 30
|
| 117 |
-
|
| 118 |
-
@app.post("/summarize")
|
| 119 |
-
def summarize_text(input_data: TextInput):
|
| 120 |
-
try:
|
| 121 |
-
|
| 122 |
-
prompt = (
|
| 123 |
-
f"Summarize the following text in at most {input_data.max_words} words:\n\n"
|
| 124 |
-
f"{input_data.text}"
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
messages = [
|
| 128 |
-
{"role": "system", "content": "You are a helpful, concise summarizer."},
|
| 129 |
-
{"role": "user", "content": prompt},
|
| 130 |
-
]
|
| 131 |
-
|
| 132 |
-
completion = client.chat.completions.create(
|
| 133 |
-
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 134 |
-
messages=messages,
|
| 135 |
-
temperature=0.7,
|
| 136 |
-
max_completion_tokens=input_data.max_words * 3,
|
| 137 |
-
top_p=1,
|
| 138 |
-
stream=False,
|
| 139 |
-
stop=None,
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
summary = completion.choices[0].message.content.strip()
|
| 143 |
-
return {"summary": summary}
|
| 144 |
-
|
| 145 |
-
except Exception as e:
|
| 146 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 94 |
raise HTTPException(status_code=500, detail=str(e))
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|