Skip to content

Commit

Permalink
Create descrivizio001-web
Browse files Browse the repository at this point in the history
  • Loading branch information
nakira974 committed Apr 20, 2023
1 parent 63ccf12 commit 8d68a8c
Show file tree
Hide file tree
Showing 36 changed files with 1,449 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,6 @@ Cargo.lock
/.idea/vcs.xml
/.idea
/Cargo.lock
*.zip
*.zip
/descrivizio001-web/obj
/descrivizio001-web/bin
12 changes: 12 additions & 0 deletions descrivizio001-web/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
9 changes: 9 additions & 0 deletions descrivizio001-web/Data/ImageDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace ImageDescriptorV2.Data;

public class ImageDescription
{
[JsonPropertyName("prediction")]
public string? Prediction { get; set; }
}
19 changes: 19 additions & 0 deletions descrivizio001-web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app

ENV ASPNETCORE_ENVIRONMENT=Development
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ImageAnalyzerV1/ImageAnalyzerV1.csproj", "ImageAnalyzerV1/"]
RUN dotnet restore "ImageAnalyzerV1/ImageAnalyzerV1.csproj"
COPY . .
WORKDIR "/src/ImageAnalyzerV1"
RUN dotnet build "ImageAnalyzerV1.csproj" -c Debug -o /app/build

FROM build AS publish
RUN dotnet publish "ImageAnalyzerV1.csproj" -c Debug -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ImageAnalyzerV1.dll"]
17 changes: 17 additions & 0 deletions descrivizio001-web/ImageAnalyzerV1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
<UserSecretsId>d6e6b670-5d12-4843-b0b2-8aab0ce27f9a</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions descrivizio001-web/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@page
@model ImageAnalyzerV1.Pages.ErrorModel

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Error</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet"/>
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true"/>
</head>

<body>
<div class="main">
<div class="content px-4">
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
</div>
</div>
</body>

</html>
26 changes: 26 additions & 0 deletions descrivizio001-web/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace ImageAnalyzerV1.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
87 changes: 87 additions & 0 deletions descrivizio001-web/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@page "/"
@using System.Text.RegularExpressions
@using ImageAnalyzerV1.Services
@inject IIMageDescriptionService _descriptionService

<PageTitle>Index</PageTitle>


<h1>Chat</h1>

<div class="row">
<div class="col-md-8">
<div class="card mb-3">
<div class="card-header">
Chat history
</div>
<div class="card-body">
<ul>
@foreach (var message in messages)
{
<li>
@if (message.IsAnImage)
{
<img src="@message.Text" width="200" height="150">
}
else
{
<strong>@message.User:</strong> @message.Text
}
</li>
}
</ul>
</div>
</div>
<form class="row" @onsubmit="@SendMessage">
<div class="col-md-9">
<input type="text" class="form-control" placeholder="Enter message" @bind="newMessage" />
@if (showErrorMessage)
{
<div class="alert alert-danger mt-2" role="alert">
Invalid URL. Please enter a valid URL that contains .jpg, .pdf, .svg, or .png.
</div>
}
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
</div>
</div>


@code {
private string newMessage = string.Empty;
private List<string> users = new List<string>();
private List<Message> messages = new List<Message>();
private bool showErrorMessage;
private bool contentSend;

private class Message
{
public string User { get; set; }
public string? Text { get; set; }
public bool IsAnImage { get; set; }
}

private async Task SendMessage()
{
string pattern = @"\bhttp\S+(?:jpg|png|gif)\b";

if (Regex.IsMatch(newMessage, pattern, RegexOptions.IgnoreCase))
{
showErrorMessage = false;
messages.Add(new Message { User = "nakira974", Text = "Describe :", IsAnImage = false });
messages.Add(new Message {User = "nakira974", Text = newMessage, IsAnImage = true});
var description = await _descriptionService.GetImageDescription(newMessage);
messages.Add(new Message {User = "descrivizio001", Text = "Sure! Let's do it...", IsAnImage = false});
messages.Add(new Message {User = "descrivizio001", Text = description?.Prediction, IsAnImage = false});
await OnAfterRenderAsync(false);
}
else
{
showErrorMessage = true;
await OnAfterRenderAsync(false);
}
}
}
8 changes: 8 additions & 0 deletions descrivizio001-web/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/"
@namespace ImageAnalyzerV1.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_Layout";
}

<component type="typeof(App)" render-mode="ServerPrerendered"/>
32 changes: 32 additions & 0 deletions descrivizio001-web/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@using Microsoft.AspNetCore.Components.Web
@namespace ImageAnalyzerV1.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<base href="~/"/>
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css"/>
<link href="css/site.css" rel="stylesheet"/>
<link href="ImageAnalyzerV1.styles.css" rel="stylesheet"/>
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered"/>
</head>
<body>
@RenderBody()

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions descrivizio001-web/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using ImageAnalyzerV1.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
// Disable HTTP redirection
serverOptions.AddServerHeader = false;
serverOptions.AllowSynchronousIO = true;
});
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<IIMageDescriptionService, ImageDescriptionService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
28 changes: 28 additions & 0 deletions descrivizio001-web/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:80",
"sslPort": 44328
}
},
"profiles": {
"ImageAnalyzerV1": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:443;http://localhost:80",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions descrivizio001-web/Services/IIMageDescriptionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using ImageDescriptorV2.Data;

namespace ImageAnalyzerV1.Services;

public interface IIMageDescriptionService
{
public Task<ImageDescription?> GetImageDescription(String imageUrl);
}
36 changes: 36 additions & 0 deletions descrivizio001-web/Services/ImageDescriptionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text.Json;
using ImageDescriptorV2.Data;

namespace ImageAnalyzerV1.Services;

public class ImageDescriptionService : IIMageDescriptionService
{
private ILogger<IIMageDescriptionService> _logger;

public ImageDescriptionService(ILogger<IIMageDescriptionService> logger)
{
_logger = logger;
}

public async Task<ImageDescription?> GetImageDescription(string imageUrl)
{
ImageDescription? result = null;
try
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Patch, "http://descrivizio001.lkh.coffee/nakira974/model/descrivizio-001/process");
request.Headers.Add("Image-Url", imageUrl);
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
result = await JsonSerializer.DeserializeAsync<ImageDescription>(await response.Content.ReadAsStreamAsync());
return result;
}
catch (Exception ex)
{
_logger.LogError(imageUrl, ": Error while descripting !");
result = new ImageDescription() {Prediction = ex.Message};
}

return result;
}
}
Loading

0 comments on commit 8d68a8c

Please sign in to comment.