File size: 1,647 Bytes
3aa3e20 232a012 3aa3e20 232a012 13b79a4 232a012 3aa3e20 232a012 3aa3e20 13b79a4 3aa3e20 13b79a4 3aa3e20 dfb5811 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch
import gradio as gr
app = FastAPI()
# Initialize model and tokenizer
model_name = "google/flan-t5-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
class Query(BaseModel):
inputs: str
@app.post("/")
async def generate(query: Query):
try:
# Tokenize input
inputs = tokenizer(query.inputs, return_tensors="pt", max_length=512, truncation=True)
# Generate response
outputs = model.generate(
inputs.input_ids,
max_length=512,
num_beams=4,
temperature=0.7,
top_p=0.9,
repetition_penalty=1.2,
early_stopping=True
)
# Decode response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return {"generated_text": response}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Gradio interface
def generate_text(prompt):
query = Query(inputs=prompt)
response = generate(query)
return response["generated_text"]
iface = gr.Interface(
fn=generate_text,
inputs=gr.Textbox(lines=2, placeholder="Enter your text here..."),
outputs="text",
title="Medical Assistant",
description="Ask me anything about medical topics!"
)
# Mount the Gradio app
app = gr.mount_gradio_app(app, iface, path="/")
if __name__ == "__main__":
import train # This will start the training process |