Spaces:
Paused
Paused
Changes in chat environment and removing .env files and readme_hf files
Browse files- .gitignore +2 -1
- src/docling_chat.py +16 -5
.gitignore
CHANGED
|
@@ -19,5 +19,6 @@ Thumbs.db
|
|
| 19 |
.gradio/
|
| 20 |
|
| 21 |
# Specific files to ignore
|
| 22 |
-
|
| 23 |
requirement.txt
|
|
|
|
|
|
| 19 |
.gradio/
|
| 20 |
|
| 21 |
# Specific files to ignore
|
| 22 |
+
README_HF.md
|
| 23 |
requirement.txt
|
| 24 |
+
.env_example
|
src/docling_chat.py
CHANGED
|
@@ -1,18 +1,29 @@
|
|
| 1 |
import openai
|
| 2 |
import os
|
| 3 |
|
|
|
|
| 4 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def chat_with_document(message, history, document_text_state):
|
| 7 |
history = history or []
|
| 8 |
history.append({"role": "user", "content": message})
|
| 9 |
|
| 10 |
context = f"Document: {document_text_state}\n\nUser: {message}"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
history.append({"role": "assistant", "content": reply})
|
| 18 |
return history, history
|
|
|
|
| 1 |
import openai
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# Load API key from environment variable
|
| 5 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 6 |
|
| 7 |
+
# Check if API key is available and print a message if not
|
| 8 |
+
if not openai.api_key:
|
| 9 |
+
print("Warning: OPENAI_API_KEY environment variable not found. Chat functionality may not work.")
|
| 10 |
+
|
| 11 |
def chat_with_document(message, history, document_text_state):
|
| 12 |
history = history or []
|
| 13 |
history.append({"role": "user", "content": message})
|
| 14 |
|
| 15 |
context = f"Document: {document_text_state}\n\nUser: {message}"
|
| 16 |
|
| 17 |
+
# Add error handling for API calls
|
| 18 |
+
try:
|
| 19 |
+
response = openai.chat.completions.create(
|
| 20 |
+
model="gpt-4o-2024-08-06",
|
| 21 |
+
messages=[{"role": "system", "content": context}] + history
|
| 22 |
+
)
|
| 23 |
+
reply = response.choices[0].message.content
|
| 24 |
+
except Exception as e:
|
| 25 |
+
reply = f"Error: Could not generate response. Please check your OpenAI API key. Details: {str(e)}"
|
| 26 |
+
print(f"OpenAI API error: {str(e)}")
|
| 27 |
+
|
| 28 |
history.append({"role": "assistant", "content": reply})
|
| 29 |
return history, history
|