Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
# Load environment variables (e.g., API keys)
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
# Initialize the OpenAI client with Hugging Face API
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
base_url="https://api-inference.huggingface.co/v1",
|
| 11 |
+
api_key=os.getenv("TOKEN")
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
model = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 15 |
+
|
| 16 |
+
def get_debyez_prompt_template(customer_message):
|
| 17 |
+
return f"""
|
| 18 |
+
Identity & Purpose
|
| 19 |
+
You are CSL Assistant, the official AI assistant for Cochin Shipyard Limited...
|
| 20 |
+
: '{customer_message}'
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
# API or CLI that will interact with the model
|
| 24 |
+
def get_response(prompt):
|
| 25 |
+
try:
|
| 26 |
+
messages = [{"role": "user", "content": prompt}]
|
| 27 |
+
stream = client.chat.completions.create(
|
| 28 |
+
model=model,
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": m["role"], "content": get_debyez_prompt_template(m["content"])}
|
| 31 |
+
for m in messages
|
| 32 |
+
],
|
| 33 |
+
temperature=0.5,
|
| 34 |
+
stream=True,
|
| 35 |
+
max_tokens=3000,
|
| 36 |
+
)
|
| 37 |
+
response = ""
|
| 38 |
+
for chunk in stream:
|
| 39 |
+
response += chunk.choices[0].delta.content or ""
|
| 40 |
+
if not response.strip():
|
| 41 |
+
response = "Sorry, I couldn't generate a response right now."
|
| 42 |
+
return response
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"An error occurred: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# Example execution
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
prompt = "What types of ships do you build?"
|
| 49 |
+
response = get_response(prompt)
|
| 50 |
+
print(response)
|