Update utils/chatbot.py
Browse files- utils/chatbot.py +12 -29
utils/chatbot.py
CHANGED
|
@@ -1,32 +1,15 @@
|
|
| 1 |
-
import openai
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
Returns:
|
| 17 |
-
answer (str): AI's response.
|
| 18 |
-
"""
|
| 19 |
-
# Combine chat history with new question
|
| 20 |
-
messages = history + [{"role": "user", "content": question}]
|
| 21 |
-
|
| 22 |
-
try:
|
| 23 |
-
response = openai.ChatCompletion.create(
|
| 24 |
-
model="gpt-3.5-turbo", # or "gpt-4" if you have access
|
| 25 |
-
messages=messages,
|
| 26 |
-
temperature=0.7,
|
| 27 |
-
max_tokens=500
|
| 28 |
-
)
|
| 29 |
-
answer = response['choices'][0]['message']['content']
|
| 30 |
-
return answer, messages + [{"role": "assistant", "content": answer}]
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return f"Error: {str(e)}", history
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
|
| 6 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 7 |
|
| 8 |
+
def get_bot_response(user_input):
|
| 9 |
+
payload = {"inputs": {"text": user_input}}
|
| 10 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 11 |
+
|
| 12 |
+
if response.status_code != 200:
|
| 13 |
+
return f"❌ Error: {response.status_code} - {response.json()}"
|
| 14 |
+
|
| 15 |
+
return response.json()[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|