Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +25 -13
- app.py +93 -162
- requirements.txt +7 -6
Dockerfile
CHANGED
|
@@ -1,13 +1,25 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.11 slim image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Install system dependencies
|
| 5 |
+
RUN apt-get update && apt-get install -y git wget && rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
# Create a non-root user
|
| 8 |
+
RUN useradd -m appuser
|
| 9 |
+
USER appuser
|
| 10 |
+
|
| 11 |
+
# Set working directory
|
| 12 |
+
WORKDIR /app
|
| 13 |
+
|
| 14 |
+
# Copy and install Python dependencies
|
| 15 |
+
COPY requirements.txt .
|
| 16 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
+
|
| 18 |
+
# Copy app files
|
| 19 |
+
COPY . .
|
| 20 |
+
|
| 21 |
+
# Expose port 8000 for FastAPI
|
| 22 |
+
EXPOSE 8000
|
| 23 |
+
|
| 24 |
+
# Start FastAPI server
|
| 25 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,162 +1,93 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 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 |
-
with
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
return px.scatter(
|
| 95 |
-
tips_data(),
|
| 96 |
-
x="total_bill",
|
| 97 |
-
y="tip",
|
| 98 |
-
color=None if color == "none" else color,
|
| 99 |
-
trendline="lowess",
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
with ui.card(full_screen=True):
|
| 103 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
| 104 |
-
"Tip percentages"
|
| 105 |
-
with ui.popover(title="Add a color variable"):
|
| 106 |
-
ICONS["ellipsis"]
|
| 107 |
-
ui.input_radio_buttons(
|
| 108 |
-
"tip_perc_y",
|
| 109 |
-
"Split by:",
|
| 110 |
-
["sex", "smoker", "day", "time"],
|
| 111 |
-
selected="day",
|
| 112 |
-
inline=True,
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
@render_plotly
|
| 116 |
-
def tip_perc():
|
| 117 |
-
from ridgeplot import ridgeplot
|
| 118 |
-
|
| 119 |
-
dat = tips_data()
|
| 120 |
-
dat["percent"] = dat.tip / dat.total_bill
|
| 121 |
-
yvar = input.tip_perc_y()
|
| 122 |
-
uvals = dat[yvar].unique()
|
| 123 |
-
|
| 124 |
-
samples = [[dat.percent[dat[yvar] == val]] for val in uvals]
|
| 125 |
-
|
| 126 |
-
plt = ridgeplot(
|
| 127 |
-
samples=samples,
|
| 128 |
-
labels=uvals,
|
| 129 |
-
bandwidth=0.01,
|
| 130 |
-
colorscale="viridis",
|
| 131 |
-
colormode="row-index",
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
plt.update_layout(
|
| 135 |
-
legend=dict(
|
| 136 |
-
orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5
|
| 137 |
-
)
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
return plt
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
ui.include_css(app_dir / "styles.css")
|
| 144 |
-
|
| 145 |
-
# --------------------------------------------------------
|
| 146 |
-
# Reactive calculations and effects
|
| 147 |
-
# --------------------------------------------------------
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
@reactive.calc
|
| 151 |
-
def tips_data():
|
| 152 |
-
bill = input.total_bill()
|
| 153 |
-
idx1 = tips.total_bill.between(bill[0], bill[1])
|
| 154 |
-
idx2 = tips.time.isin(input.time())
|
| 155 |
-
return tips[idx1 & idx2]
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
@reactive.effect
|
| 159 |
-
@reactive.event(input.reset)
|
| 160 |
-
def _():
|
| 161 |
-
ui.update_slider("total_bill", value=bill_rng)
|
| 162 |
-
ui.update_checkbox_group("time", selected=["Lunch", "Dinner"])
|
|
|
|
| 1 |
+
from peft import AutoPeftModelForCausalLM
|
| 2 |
+
import asyncio
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
import uvicorn
|
| 5 |
+
import os
|
| 6 |
+
import nest_asyncio
|
| 7 |
+
import socket
|
| 8 |
+
import json
|
| 9 |
+
from transformers import pipeline, AutoTokenizer
|
| 10 |
+
import time
|
| 11 |
+
import torch
|
| 12 |
+
import requests
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
model_ask = AutoPeftModelForCausalLM.from_pretrained(
|
| 15 |
+
"robertthecreator/assistant-chatbot",
|
| 16 |
+
device_map="auto",
|
| 17 |
+
torch_dtype=torch.float16
|
| 18 |
+
)
|
| 19 |
+
tokenizer_ask = AutoTokenizer.from_pretrained("robertthecreator/assistant-chatbot")
|
| 20 |
+
tokenizer_ask.pad_token = tokenizer_ask.eos_token
|
| 21 |
+
model_ask.eval()
|
| 22 |
+
api_key = os.getenv("TAVILY_API_KEY", "default-key")
|
| 23 |
+
|
| 24 |
+
def get_from_web(query, k=3):
|
| 25 |
+
context = ""
|
| 26 |
+
response = requests.post(
|
| 27 |
+
"https://api.tavily.com/search",
|
| 28 |
+
headers={"Content-Type": "application/json"},
|
| 29 |
+
json={"query": query, "api_key": api_key}
|
| 30 |
+
)
|
| 31 |
+
data = response.json()
|
| 32 |
+
for result in data["results"]:
|
| 33 |
+
print(result["content"])
|
| 34 |
+
context += result["content"] + "\n"
|
| 35 |
+
return context
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@app.post("/ask")
|
| 39 |
+
def ask(text: str):
|
| 40 |
+
global context
|
| 41 |
+
print("starting...")
|
| 42 |
+
context = """You're an AI assistant designed to help me with every day task, productivity and engineering. Youre name is JARVIS. Use the context you have to answer any questions."""
|
| 43 |
+
context = get_from_web(text) + "\n" + context
|
| 44 |
+
print(context)
|
| 45 |
+
formatted_prompt = f"""CONTEXT: {context}\nPROMPTER: {text}\nASSISTANT:"""
|
| 46 |
+
inputs = tokenizer_ask(formatted_prompt, return_tensors="pt").to(model_ask.device)
|
| 47 |
+
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
outputs = model_ask.generate(
|
| 50 |
+
**inputs,
|
| 51 |
+
max_new_tokens=250,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
top_p=0.9,
|
| 54 |
+
top_k=50,
|
| 55 |
+
repetition_penalty=1.2,
|
| 56 |
+
do_sample=True,
|
| 57 |
+
pad_token_id=tokenizer_ask.eos_token_id,
|
| 58 |
+
eos_token_id=tokenizer_ask.eos_token_id,
|
| 59 |
+
early_stopping=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
response = tokenizer_ask.decode(outputs[0], skip_special_tokens=True)
|
| 64 |
+
print(response)
|
| 65 |
+
|
| 66 |
+
if "ASSISTANT:" in response:
|
| 67 |
+
response = response.split("ASSISTANT:")[-1].strip()
|
| 68 |
+
|
| 69 |
+
response = response.split('\r')[0]
|
| 70 |
+
response = response.split('PROMPTER')[0]
|
| 71 |
+
response = response.split('ROLE:')[0]
|
| 72 |
+
response = response.split('<|user|>')[0]
|
| 73 |
+
response = response.split('User:')[0]
|
| 74 |
+
|
| 75 |
+
return response
|
| 76 |
+
|
| 77 |
+
async def start():
|
| 78 |
+
s = socket.socket()
|
| 79 |
+
s.bind(('', 0))
|
| 80 |
+
port = s.getsockname()[1]
|
| 81 |
+
s.close()
|
| 82 |
+
|
| 83 |
+
config = uvicorn.Config(
|
| 84 |
+
app,
|
| 85 |
+
host="0.0.0.0",
|
| 86 |
+
port=port,
|
| 87 |
+
log_level="info"
|
| 88 |
+
)
|
| 89 |
+
server = uvicorn.Server(config)
|
| 90 |
+
|
| 91 |
+
await server.serve()
|
| 92 |
+
|
| 93 |
+
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
peft
|
| 6 |
+
requests
|
| 7 |
+
nest_asyncio
|