Compare commits

..

4 Commits
tests ... main

Author SHA1 Message Date
Falko Victor Habel e960ba886b Merge pull request 'develop' (#18) from develop into main
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 9m53s Details
Reviewed-on: Berufsschule/Veracity_AI#18
2024-12-05 11:16:03 +00:00
Falko Victor Habel 1715325699 updated pytests for new langchain libary
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 9m54s Details
2024-12-05 09:41:04 +01:00
Falko Victor Habel f6e12e0469 updated code
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 5m27s Details
2024-12-05 09:05:17 +01:00
Falko Victor Habel 30c0091795 Merge pull request 'tests' (#17) from tests into develop
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 9m58s Details
Reviewed-on: Berufsschule/Veracity_AI#17
2024-11-01 07:46:03 +00:00
5 changed files with 27 additions and 27 deletions

View File

@ -40,4 +40,4 @@ To use the Fake News Checker application, follow these steps:
## License ## License
This application is licensed under the MIT license. See the [LICENSE](LICENSE) file for more details. This application is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International. See the [LICENSE](LICENSE) file for more details.

BIN
docs/use-case-diagram.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

View File

@ -1,4 +1,5 @@
from langchain_community.llms import Ollama from langchain_ollama import ChatOllama
from langchain_core.messages import AIMessage
import os import os
class ArticleRater: class ArticleRater:
@ -6,6 +7,8 @@ class ArticleRater:
self.client = "https://ai.fabelous.app/v1/ollama/generic" self.client = "https://ai.fabelous.app/v1/ollama/generic"
self.token = self._get_token() self.token = self._get_token()
self.headers = {"Authorization": f"Token {self.token}"} self.headers = {"Authorization": f"Token {self.token}"}
self.model = "phi3.5:3.8b-mini-instruct-q4_K_M"
self.llm = ChatOllama(model=self.model, client_kwargs={'headers': self.headers}, base_url=self.client)
def _get_token(self): def _get_token(self):
if os.path.exists("Token/Token.txt"): if os.path.exists("Token/Token.txt"):
@ -15,22 +18,16 @@ class ArticleRater:
return None return None
def get_response(self, article, result, confidence): def get_response(self, article, result, confidence):
ollama_params = { messages = [
"base_url": self.client, ("system", """Ein Mashine Learning Model hat einen Text bewertet, ob es sich um FakeNews handelt oder um Reale News.
"model": "mistral-nemo:12b-instruct-2407-q8_0",
"headers": self.headers,
"system": """Ein Mashine Learning Model hat einen Text bewertet, ob es sich um FakeNews handelt oder um Reale News.
Erkläre in 1-2 Sätzen warum dieses Modell zu dieser Entscheidung. Erkläre in 1-2 Sätzen warum dieses Modell zu dieser Entscheidung.
DU SOLLST KEINE ÜBERSCHRIFTEN oder ähnliches ERKLÄREN. Du erhählst einen TEXT und sollst erklären wie das RESULTAT zustande kam""" DU SOLLST KEINE ÜBERSCHRIFTEN oder ähnliches ERKLÄREN. Du erhählst einen TEXT und sollst erklären wie das RESULTAT zustande kam"""),
} ("human", f"{article}, result: {result}, confidence {confidence}")
]
message = (f"{article}, result: {result}, confidence {confidence}")
# Initialize the Ollama object with the prepared parameters
llm = Ollama(**ollama_params)
# Return the response stream # Return the response stream
return llm.stream(message) return self.llm.stream(messages)
# Usage # Usage
if __name__ == "__main__": if __name__ == "__main__":
@ -43,4 +40,4 @@ if __name__ == "__main__":
# Capture the stream response # Capture the stream response
response_stream = article_rater.get_response(article, result, confidence=confidence) response_stream = article_rater.get_response(article, result, confidence=confidence)
for chunk in response_stream: for chunk in response_stream:
print(chunk, end='', flush=True) print(chunk.content, end="")

View File

@ -67,7 +67,7 @@ class MainFrameController:
response_stream = self.rater.get_response(text_data.text, text_data.result, confidence) response_stream = self.rater.get_response(text_data.text, text_data.result, confidence)
for chunk in response_stream: for chunk in response_stream:
self.frame.output_textbox.insert("end", chunk) self.frame.output_textbox.insert("end", chunk.content)
self.frame.output_textbox.see("end") self.frame.output_textbox.see("end")
self.frame.update_idletasks() self.frame.update_idletasks()

View File

@ -3,7 +3,6 @@ import os
import pytest import pytest
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
# Add the src directory to the Python path
src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'src')) src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
sys.path.insert(0, src_dir) sys.path.insert(0, src_dir)
@ -34,7 +33,6 @@ def test_init(controller):
assert isinstance(controller.db, MagicMock) assert isinstance(controller.db, MagicMock)
assert isinstance(controller.rater, MagicMock) assert isinstance(controller.rater, MagicMock)
def test_get_text_data(controller): def test_get_text_data(controller):
controller.frame.entry_url.get.return_value = "https://example.com" controller.frame.entry_url.get.return_value = "https://example.com"
controller.frame.input_textbox.get.return_value = "Sample text" controller.frame.input_textbox.get.return_value = "Sample text"
@ -65,13 +63,19 @@ def test_press_check_button(controller, result, expected_result_color, confidenc
controller._predict = MagicMock(return_value=text_data) controller._predict = MagicMock(return_value=text_data)
controller._add_to_db = MagicMock() controller._add_to_db = MagicMock()
controller.update_provider_list = MagicMock() controller.update_provider_list = MagicMock()
controller.rater.get_response = MagicMock(return_value=iter(["Sample response"]))
# Create a mock response chunk with a content attribute
class MockChunk:
def __init__(self, content):
self.content = content
# Mocking get_response to return a list of MockChunk instances
mock_response = [MockChunk("Sample response")]
controller.rater.get_response = MagicMock(return_value=mock_response)
# Mock frame and its subcomponents # Mock frame and its subcomponents
controller.frame = MagicMock() controller.frame = MagicMock()
controller.frame.result_label = MagicMock()
controller.frame.result_label.configure = MagicMock() controller.frame.result_label.configure = MagicMock()
controller.frame.confidence_label = MagicMock()
controller.frame.confidence_label.configure = MagicMock() controller.frame.confidence_label.configure = MagicMock()
controller.frame.output_textbox = MagicMock() controller.frame.output_textbox = MagicMock()
controller.frame.output_textbox.insert = MagicMock() controller.frame.output_textbox.insert = MagicMock()
@ -84,8 +88,9 @@ def test_press_check_button(controller, result, expected_result_color, confidenc
controller.frame.confidence_label.configure.assert_any_call(text=f"{confidence * 100:.2f}%") controller.frame.confidence_label.configure.assert_any_call(text=f"{confidence * 100:.2f}%")
controller.frame.confidence_label.configure.assert_any_call(fg_color=expected_confidence_color) controller.frame.confidence_label.configure.assert_any_call(fg_color=expected_confidence_color)
# Additional assertion to verify that the output textbox is updated # Additional assertion to verify that the output textbox is updated with the response content
controller.frame.output_textbox.insert.assert_called_with("end", "Sample response") controller.frame.output_textbox.insert.assert_called_with("end", mock_response[0].content)
def test_predict(controller): def test_predict(controller):
text_data = TextData(text="Sample text") text_data = TextData(text="Sample text")
@ -102,15 +107,13 @@ def test_predict(controller):
assert result.is_fake_news == False assert result.is_fake_news == False
def test_add_to_db(controller): def test_add_to_db(controller):
# Adjust the fields to match the actual insert_data arguments
text_data = TextData(url="https://example.com", provider="Example Provider", is_fake_news=False) text_data = TextData(url="https://example.com", provider="Example Provider", is_fake_news=False)
controller._add_to_db(text_data) controller._add_to_db(text_data)
controller.db.insert_data.assert_called_with( controller.db.insert_data.assert_called_with(
url="https://example.com", url="https://example.com",
anbieter="example.com", # Adjusted to match actual expected field name anbieter="example.com",
is_fake_news=False is_fake_news=False
) )
if __name__ == "__main__": if __name__ == "__main__":
pytest.main([__file__]) pytest.main([__file__])