Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -7,13 +7,19 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 7 |
from langchain_huggingface import HuggingFacePipeline
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
import torch
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
app = FastAPI(
|
| 12 |
title="Phi-3 Code Explainer API",
|
| 13 |
description="Explains Python code using the Phi-3 Mini model and LangChain.",
|
| 14 |
version="1.0"
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"],
|
|
@@ -22,28 +28,38 @@ app.add_middleware(
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
|
|
|
| 25 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 27 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
-
model_id, torch_dtype=torch.float16, device_map="auto"
|
| 29 |
-
)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
generator = pipeline(
|
| 32 |
"text-generation",
|
| 33 |
model=model,
|
| 34 |
tokenizer=tokenizer,
|
| 35 |
max_new_tokens=256,
|
| 36 |
return_full_text=False,
|
| 37 |
-
do_sample=
|
|
|
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
llm = HuggingFacePipeline(pipeline=generator)
|
| 41 |
-
|
| 42 |
prompt = ChatPromptTemplate.from_template(
|
| 43 |
-
"### Instruction
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
| 46 |
parser = StrOutputParser()
|
| 47 |
chain = prompt | llm | parser
|
| 48 |
|
|
|
|
| 49 |
add_routes(app, chain, path="/explain")
|
|
|
|
|
|
| 7 |
from langchain_huggingface import HuggingFacePipeline
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
import torch
|
| 10 |
+
import transformers
|
| 11 |
|
| 12 |
+
# Reduce logs
|
| 13 |
+
transformers.logging.set_verbosity_error()
|
| 14 |
+
|
| 15 |
+
# Create FastAPI app
|
| 16 |
app = FastAPI(
|
| 17 |
title="Phi-3 Code Explainer API",
|
| 18 |
description="Explains Python code using the Phi-3 Mini model and LangChain.",
|
| 19 |
version="1.0"
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# CORS settings
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
| 25 |
allow_origins=["*"],
|
|
|
|
| 28 |
allow_headers=["*"],
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Load model/tokenizer safely for CPU-only spaces
|
| 32 |
model_id = "microsoft/phi-3-mini-4k-instruct"
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# If GPU available use float16, else default
|
| 36 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
+
model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
|
| 38 |
+
|
| 39 |
+
# Generation pipeline
|
| 40 |
generator = pipeline(
|
| 41 |
"text-generation",
|
| 42 |
model=model,
|
| 43 |
tokenizer=tokenizer,
|
| 44 |
max_new_tokens=256,
|
| 45 |
return_full_text=False,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
temperature=0.7
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# LangChain pipeline
|
| 51 |
llm = HuggingFacePipeline(pipeline=generator)
|
|
|
|
| 52 |
prompt = ChatPromptTemplate.from_template(
|
| 53 |
+
"### Instruction:
|
| 54 |
+
Explain this Python code step-by-step:
|
| 55 |
+
Use code with caution
|
| 56 |
+
{code}
|
| 57 |
|
| 58 |
+
### Explanation:"
|
| 59 |
+
)
|
| 60 |
parser = StrOutputParser()
|
| 61 |
chain = prompt | llm | parser
|
| 62 |
|
| 63 |
+
# Add route
|
| 64 |
add_routes(app, chain, path="/explain")
|
| 65 |
+
|