Spaces:
Runtime error
Runtime error
Samik-Pandit
commited on
Commit
·
acbcb99
1
Parent(s):
2670c21
All
Browse files- Dockerfile +1 -1
- main.py +113 -0
- requirements.txt +3 -0
Dockerfile
CHANGED
@@ -8,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
8 |
|
9 |
COPY . .
|
10 |
|
11 |
-
CMD ["uvicorn", "
|
|
|
8 |
|
9 |
COPY . .
|
10 |
|
11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# main.py
|
2 |
+
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from typing import List, Optional
|
6 |
+
import openai
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Define models for data validation
|
11 |
+
class Summarize(BaseModel):
|
12 |
+
diagnose: str
|
13 |
+
cause: str
|
14 |
+
solution: str
|
15 |
+
|
16 |
+
class StoredResponse(BaseModel):
|
17 |
+
selected_response: str
|
18 |
+
diagnose: str
|
19 |
+
cause: str
|
20 |
+
solution: str
|
21 |
+
|
22 |
+
# Initialize OpenAI API key
|
23 |
+
openai.api_key = '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'
|
24 |
+
|
25 |
+
# Database to store stored responses (simulated in-memory database)
|
26 |
+
stored_responses = []
|
27 |
+
|
28 |
+
# Function to check if a stored response exists for a given input
|
29 |
+
def find_stored_response(diagnose: str, cause: str, solution: str) -> Optional[StoredResponse]:
|
30 |
+
for response in stored_responses:
|
31 |
+
if (
|
32 |
+
response.diagnose == diagnose
|
33 |
+
and response.cause == cause
|
34 |
+
and response.solution == solution
|
35 |
+
):
|
36 |
+
return response
|
37 |
+
return None
|
38 |
+
|
39 |
+
# Summarize endpoint
|
40 |
+
@app.post("/summarize/", response_model=dict)
|
41 |
+
async def summarize(item: Summarize):
|
42 |
+
# Check if a stored response exists for the input
|
43 |
+
existing_response = find_stored_response(item.diagnose, item.cause, item.solution)
|
44 |
+
if existing_response:
|
45 |
+
return [existing_response.selected_response]
|
46 |
+
|
47 |
+
# Use OpenAI's GPT-3 to generate a summary
|
48 |
+
user_input = (
|
49 |
+
f"Diagnose: {item.diagnose}\n"
|
50 |
+
f"Cause: {item.cause}\n"
|
51 |
+
f"Solution: {item.solution}\n"
|
52 |
+
)
|
53 |
+
prompt = "Translate and summarize Norwegian inputs about 'Diagnose', 'Cause', and 'Løsning og resultat' into English outputs of 'Diagnosis' and 'Solution', each strictly limited to 72 characters. When using domain-specific terminology, refer to the dictionary to understand or include relevant acronyms: acronym_dictionary = { OBC: On-Board Charger, E_VCU: Electronic Vehicle Control Unit, TSB: Technical Service Bulletin, (Examples) U2FFF / B1004 96 / U1F2F 87 / P1B74 / P000A 00: This is an error code that was registered in the fault log of the vehicle., TSB D1BW011FQ0: Code, HV battery: High Volt battery, ECU: Engine Control Unit, VOR: Vehicle Off Road, DID: Deal Issue Detection, DID-I: Dealer Issue Detection Incident, DID-A: Dealer Issue Detection Assistance, DID-S: Dealer Issue Detection Safety, EV-ECU: Electric Vehicle ECU, BMU or TBMU: Traction Battery ECU, HPCU: Hybrid Control Unit, MCU: Inverter, iSolve: Repair Solution, DPF: Diesel Particle Filter, EGR: Exhaust Gas Recirculation, DA: Documents (Sharing Platform), Reman: Remanufactured Parts, DTC: Diagnostic Trouble Code, DARS: Documents }.Always mention TSB, mentioned codes and Kilometers or units with details.GIVE 3 DIFFERENT RESPONSES IN NEW LINES WITH SPECIAL NUMBERINGS LIKE #1 #2 #3. Example: #1. Diagnosis: Text. \nSolution: Text.\n#2. Diagnosis: Text. \nSolution: Text.\n#3. Diagnosis: Text. \nSolution: Text."
|
54 |
+
|
55 |
+
response = openai.Completion.create(
|
56 |
+
engine="text-davinci-002",
|
57 |
+
prompt=prompt + "\n" + user_input,
|
58 |
+
max_tokens=3000
|
59 |
+
)
|
60 |
+
|
61 |
+
gpt_response = response.choices[0].text.strip()
|
62 |
+
response_parts = gpt_response.split('\n#')
|
63 |
+
|
64 |
+
if len(response_parts) >= 3:
|
65 |
+
response1 = response_parts[0]
|
66 |
+
response2 = response_parts[1]
|
67 |
+
response3 = response_parts[2]
|
68 |
+
else:
|
69 |
+
response1 = response_parts[0] if len(response_parts) >= 1 else ""
|
70 |
+
response2 = response_parts[1] if len(response_parts) >= 2 else ""
|
71 |
+
response3 = ""
|
72 |
+
|
73 |
+
if response1.startswith("#"):
|
74 |
+
response1 = response1[1:]
|
75 |
+
else:
|
76 |
+
response1 = response1
|
77 |
+
|
78 |
+
response1 = response1.strip()
|
79 |
+
response2 = response2.strip()
|
80 |
+
response3 = response3.strip()
|
81 |
+
|
82 |
+
response_dict = {
|
83 |
+
"Response 1": response1,
|
84 |
+
"Response 2": response2,
|
85 |
+
"Response 3": response3
|
86 |
+
}
|
87 |
+
|
88 |
+
return response_dict
|
89 |
+
|
90 |
+
# Store Response endpoint
|
91 |
+
@app.post("/store-response/", response_model=StoredResponse)
|
92 |
+
async def store_selected_response(item: StoredResponse):
|
93 |
+
# Check if a stored response with the same input already exists
|
94 |
+
existing_response = find_stored_response(item.diagnose, item.cause, item.solution)
|
95 |
+
if existing_response:
|
96 |
+
# Update the existing response
|
97 |
+
existing_response.selected_response = item.selected_response
|
98 |
+
return existing_response
|
99 |
+
|
100 |
+
# If no matching stored response, create a new one
|
101 |
+
stored_responses.append(item)
|
102 |
+
return item
|
103 |
+
|
104 |
+
# Check Response endpoint
|
105 |
+
@app.post("/check-response/", response_model=StoredResponse)
|
106 |
+
async def check_stored_response(item: Summarize):
|
107 |
+
# Check if a stored response exists for the input
|
108 |
+
stored_response = find_stored_response(item.diagnose, item.cause, item.solution)
|
109 |
+
if stored_response:
|
110 |
+
return stored_response
|
111 |
+
|
112 |
+
# If no stored response found, raise an HTTPException with 404 status code
|
113 |
+
raise HTTPException(status_code=404, detail="No stored response found")
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
greenlet==3.0.1
|
2 |
+
SQLAlchemy==2.0.23
|
3 |
+
typing_extensions==4.8.0
|