sanvlr12 / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
0 stars 0 forks source link

Trainning result in Error message - No model found. Model must first be trained. [404] #130

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

After authentication when I am trying to access the Training status, I am 
getting the following exception. 

Google.Apis.Requests.RequestError
No model found. Model must first be trained. [404]
Errors [
    Message[No model found. Model must first be trained.] Location[ - ] Reason[notFound] Domain[global]

Here is the Code I am using at my end. 

public partial class _Default : System.Web.UI.Page
    {
        private static PredictionService _service; // We don't need individual service instances for each client.

        private IAuthorizationState _state;
        private OAuth2Authenticator<WebServerClient> _authenticator;

        private IAuthorizationState AuthState
        {
            get
            {
                return _state ?? HttpContext.Current.Session["AUTH_STATE"] as IAuthorizationState;
            }
        }

        private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
        {
            // Register the authenticator.
            var provider = new WebServerClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = ClientCredentials.ClientID;
            provider.ClientSecret = ClientCredentials.ClientSecret;
            var authenticator = new OAuth2Authenticator<WebServerClient>(provider, GetAuthentication);
            //{ NoCaching = true }; // TODO(mlinder): Uncomment when CL is merged.
            return authenticator;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CommandLine.EnableExceptionHandling();
            CommandLine.DisplayGoogleSampleHeader("Prediction API");
            _authenticator = CreateAuthenticator();
            CommandLine.WriteLine();

            if (_service == null)
            {
                _service = new PredictionService(_authenticator);
            }

            // Check if we received OAuth2 credentials with this request; if yes: parse it.
            if (HttpContext.Current.Request["code"] != null)
            {
                _authenticator.LoadAccessToken();
            }

            RunPrediction(_service);

           CommandLine.PressAnyKeyToExit();
        }

        private IAuthorizationState GetAuthentication(WebServerClient client)
        {
            // If this user is already authenticated, then just return the auth state.
            IAuthorizationState state = AuthState;
            if (state != null)
            {
                return state;
            }

            // Check if an authorization request already is in progress.
            state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
            if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
            {
                // Store and return the credentials.
                HttpContext.Current.Session["AUTH_STATE"] = _state = state;
                return state;
            }

            string scope = PredictionService.Scopes.Prediction.GetStringValue();  

           OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
           response.Send(); // Will throw a ThreadAbortException to prevent sending another response.
           return null;
        }

        private static IAuthorizationState GetAuthentication(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            const string STORAGE = "google.samples.dotnet.prediction";
            const string KEY = "AF41sdBra7ufra)VD:@#A#a++=3e";
            string scope = PredictionService.Scopes.Prediction.GetStringValue();

            // Check if there is a cached refresh token available.
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
                }
            }

            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
            AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
            return state;
        }

        private static void RunPrediction(PredictionService service)
        {
            // Train the service with the existing bucket data.
            CommandLine.WriteAction("Performing training of the service ...");
            CommandLine.WriteResult("Bucket", ClientCredentials.BucketPath);
            Training training = new Training() { Id = ClientCredentials.BucketPath };

            training = service.Training.Insert(training).Fetch();

            Thread.Sleep(2000); 

            // Wait until the training is complete.
            while ((training= service.Training.Get(training.Id).Fetch()).TrainingStatus == "RUNNING")
            {
                CommandLine.Write("..");
                Thread.Sleep(1000);
            }
            CommandLine.WriteLine();
            CommandLine.WriteAction("Training complete!");
            CommandLine.WriteLine();

            // Make a prediction.
            CommandLine.WriteAction("Performing a prediction...");
            string text = "mucho bueno";
            CommandLine.RequestUserInput("Text to analyze", ref text);

            var input = new Input { InputValue = new Input.InputData { CsvInstance = new List<string> { text } } };
            Output result = service.Training.Predict(input, training.Id).Fetch();
            CommandLine.WriteResult("Language", result.OutputLabel);
        }

    }

Exception is thrown on the following line 
training= service.Training.Get(training.Id).Fetch()

What version of the product are you using? On what operating system?
I am testing on Vista and I am using the Prediction API version 3

Attached is my bucket file that I using to trained my Model 

Original issue reported on code.google.com by neeraj....@gmail.com on 28 Aug 2011 at 3:49

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the bug report. I am currently looking into the issue -- It looks 
like there is a bug with the escaping of path parameters. For the moment you 
could try using "%2F" instead of "/" in the bucket path.

Original comment by mlin...@google.com on 29 Aug 2011 at 8:24

GoogleCodeExporter commented 9 years ago
Seems like the URI constructor unescapes the %2F into a /, and thereby causes 
the issue.

Original comment by mlin...@google.com on 29 Aug 2011 at 8:38

GoogleCodeExporter commented 9 years ago
Hi, 

Thanks for the response. 

I tried setting by bucket path using %2F. but now I am getting another error 
while making the training request 

Google.Apis.Requests.RequestError
 [500]
No individual errors

my current bucket path is : "449122138224%2Flanguage_id.csv"
Please note that I tried this on version 1.2 and it is working fine over there. 
I am seeing the same issue with API explorer too.

Thanks 

Neeraj Dev

Original comment by neeraj....@gmail.com on 30 Aug 2011 at 2:53

GoogleCodeExporter commented 9 years ago
We have discovered that this is actually a bug in the System.Uri class, which 
will prevent you from using an escaped "%2F" in any uri. We are working on 
implementing a workaround for it. 

I just tested the behavior of the API Explorer. It works fine there. Have you 
enabled private access? You should use a simple "/" there, as the explorer will 
do the escaping for you. 

The Codereview which is associated with this issue can be found here:
  http://codereview.appspot.com/4969050/

Original comment by mlin...@google.com on 30 Aug 2011 at 3:00

GoogleCodeExporter commented 9 years ago
I will look forward for the workaround. Any ETA on this so that I can update my 
client accordingly. 

For API Explorer: Yes I enabled private access. I am using the following URL 
https://code.google.com/apis/explorer/#_s=prediction&_v=v1.3&_m=training.insert&
fields=449122138224/language_id.csv

I am using "449122138224/language_id.csv" for executing the call and I am 
getting following as the response. 

Response

500 Internal Server Error

- Show headers -

{
"error": {
"code": 500,
"message": null
}
}

Original comment by neeraj....@gmail.com on 30 Aug 2011 at 3:09

GoogleCodeExporter commented 9 years ago
The issue should have been fixed with the latest release. You can download it 
here:

  http://code.google.com/p/google-api-dotnet-client/wiki/Downloads?tm=2

Use 
  BucketPath = "449122138224/language_id.csv"

and make sure that this file exists on Google Storage. Please confirm that this 
issue has been resolved. I would also recommend starting with the 
Prediction.Simple sample again, as I also changed the layout of that sample 
slightly in the process of fixing this issue.

@ Google API Explorer: You specified your BucketPath in the "Fields"-parameter, 
which is only used for partial responses. Specify it by clicking on "Add 
request body", then adding the "id" field and entering your path there.

Thanks for your patience!

Original comment by mlin...@google.com on 30 Aug 2011 at 6:40

GoogleCodeExporter commented 9 years ago
Thanks for the feedback. The latest version certainly fixed the issue for the 
desktop version. I can run the sample prediction code for desktop. 

But the same fails for Web project. 

I am getting the following exception while training the data 

"You must provide a request body if you set ContentLength>0 or 
SendChunked==true.  Do this by calling [Begin]GetRequestStream before 
[Begin]GetResponse." 

on line :  training = service.Training.Insert(training).Fetch(); 

Here is the updated code as per the latest revision

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Threading;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Prediction.v1_3;
using Google.Apis.Prediction.v1_3.Data;
using Google.Apis.Samples.Helper;
using Google.Apis.Util;
using Prediction.Simple;

using DotNetOpenAuth.Messaging;

using Google.Apis.Discovery;
using GoogleRequests = Google.Apis.Requests;

using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2.ChannelElements;
using DotNetOpenAuth.OAuth2.Messages;

using System.Configuration;
using System.IO;

namespace GooglePredicition
{
    public partial class _Default : System.Web.UI.Page
    {
        private static PredictionService _service; // We don't need individual service instances for each client.

        private IAuthorizationState _state;
        private OAuth2Authenticator<WebServerClient> _authenticator;

        private IAuthorizationState AuthState
        {
            get
            {
                return _state ?? HttpContext.Current.Session["AUTH_STATE"] as IAuthorizationState;
            }
        }

        private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
        {
            // Register the authenticator.
            var provider = new WebServerClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = ClientCredentials.ClientID;
            provider.ClientSecret = ClientCredentials.ClientSecret;
            var authenticator = new OAuth2Authenticator<WebServerClient>(provider, GetAuthentication);
            //{ NoCaching = true }; // TODO(mlinder): Uncomment when CL is merged.
            return authenticator;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CommandLine.EnableExceptionHandling();
            _authenticator = CreateAuthenticator();

            if (_service == null)
            {
                _service = new PredictionService(_authenticator);
            }

            // Check if we received OAuth2 credentials with this request; if yes: parse it.
            if (HttpContext.Current.Request["code"] != null)
            {
                _authenticator.LoadAccessToken();
            }

            RunPrediction(_service);

           CommandLine.PressAnyKeyToExit();
        }

        private IAuthorizationState GetAuthentication(WebServerClient client)
        {
            // If this user is already authenticated, then just return the auth state.
            IAuthorizationState state = AuthState;
            if (state != null)
            {
                return state;
            }

            // Check if an authorization request already is in progress.
            state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
            if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
            {
                // Store and return the credentials.
                HttpContext.Current.Session["AUTH_STATE"] = _state = state;
                return state;
            }

            string scope = PredictionService.Scopes.Prediction.GetStringValue();  

           OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
           response.Send(); // Will throw a ThreadAbortException to prevent sending another response.
           return null;
        }

        private static IAuthorizationState GetAuthentication(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            const string STORAGE = "google.samples.dotnet.prediction";
            const string KEY = "AF41sdBra7ufra)VD:@#A#a++=3e";
            string scope = PredictionService.Scopes.Prediction.GetStringValue();

            // Check if there is a cached refresh token available.
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
                }
            }

            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
            AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
            return state;
        }

        private static void RunPrediction(PredictionService service)
        {
            // Train the service with the existing bucket data.
            CommandLine.WriteAction("Performing training of the service ...");
            CommandLine.WriteResult("Bucket", ClientCredentials.BucketPath);
            string id = ClientCredentials.BucketPath;

            Training training = new Training() { Id = id};

            training = service.Training.Insert(training).Fetch();

            // Wait until the training is complete.
            while (training.TrainingStatus == "RUNNING")
            {
                CommandLine.Write("..");
                Thread.Sleep(1000);
                training = service.Training.Get(id).Fetch();
            }

            // Make a prediction.
            string text = "Seattle , 283, cloudy";
           // CommandLine.RequestUserInput("Text to analyze", ref text);

            var input = new Input { InputValue = new Input.InputData { CsvInstance = new List<string> { text } } };
            Output result = service.Training.Predict(input, training.Id).Fetch();
            CommandLine.WriteResult("Language", result.OutputLabel);
        }

    }
}

Original comment by neeraj....@gmail.com on 31 Aug 2011 at 7:44

GoogleCodeExporter commented 9 years ago
I just copied the prediction code into the Tasks.ASP.NET.SimpleOAuth2 sample:

http://pastebin.com/9MLdSsEC

Making a prediction works without any problem. Your error might be caused by 
making a prediction in the Page_Load event. At this point it is likely that you 
might not be authenticated & allowed to make a request.

Original comment by mlin...@google.com on 31 Aug 2011 at 8:12

GoogleCodeExporter commented 9 years ago
Hi, 

Tried the the example on http://pastebin.com/9MLdSsEC. Still getting the same 
error. 

System.Net.ProtocolViolationException: You must provide a request body if you 
set ContentLength>0 or SendChunked==true.  Do this by calling 
[Begin]GetRequestStream before [Begin]GetResponse. 
  at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state) 
  at Google.Apis.Requests.Request.InternalBeginExecuteRequest(AsyncExecutionState state) in c:\Users\mlinder\Desktop\default\Tools\BuildRelease\bin\Debug\8-30-2011\default\Src\GoogleApis\Apis\Requests\Request.cs:line 312 
  at Google.Apis.Requests.Request.ExecuteRequestAsync(Action`1 responseHandler) in c:\Users\mlinder\Desktop\default\Tools\BuildRelease\bin\Debug\8-30-2011\default\Src\GoogleApis\Apis\Requests\Request.cs:line 405 
  at Google.Apis.Requests.ServiceRequest`1.GetAsyncResponse(Action`1 responseHandler) in c:\Users\mlinder\Desktop\default\Tools\BuildRelease\bin\Debug\8-30-2011\default\Src\GoogleApis\Apis\Requests\ServiceRequest.cs:line 162 
  at Google.Apis.Requests.ServiceRequest`1.GetResponse() in c:\Users\mlinder\Desktop\default\Tools\BuildRelease\bin\Debug\8-30-2011\default\Src\GoogleApis\Apis\Requests\ServiceRequest.cs:line 177 
  at Google.Apis.Requests.ServiceRequest`1.Fetch() in c:\Users\mlinder\Desktop\default\Tools\BuildRelease\bin\Debug\8-30-2011\default\Src\GoogleApis\Apis\Requests\ServiceRequest.cs:line 203 
  at Tasks.ASP.NET.SimpleOAuth2._Default.FetchTaskslists() in C:\Users\Dev\Documents\Visual Studio 2010\Projects\GooglePredicition\GooglePredicition\Default.aspx.cs:line 121 

Attached is the my complete project. 

Original comment by neeraj....@gmail.com on 1 Sep 2011 at 5:05

Attachments:

GoogleCodeExporter commented 9 years ago
Hi..

Please let me know if you are looking into this or this is something that I 
need to look at my end.

Thanks 

Neeraj Dev

Original comment by neeraj....@gmail.com on 2 Sep 2011 at 5:52

GoogleCodeExporter commented 9 years ago
I am trying to call the Google LatitudeService using WCF. I can get it by 
ASP.NET but while shifted codes to WCF. 
 OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });

                response.Send(); // throws exception.

Please suggest.

Regards,
Soumen Dey, rimbik@gmail.com

Original comment by rim...@gmail.com on 5 Sep 2011 at 8:56

Attachments:

GoogleCodeExporter commented 9 years ago
bug is in AttachBody(WebRequest request) method.  This code is calling 
BeginGetRequestStream which is Async. EndAttachBody is called but after 
ExecuteRequestAsync is called.  Change BeginGetRequestStream to 
GetRequestStream and move code from EndAttachBody into AttachBody method.

Original comment by andrewad...@gmail.com on 10 Jan 2012 at 9:40

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks for finding the bug in AttachBody.() This really helped me out. 

Should this be registered as a new issue? I got the exception with every 
Fetch() of an insert request. How can anyone post data to the API as long as 
this bug is there?

Original comment by eyeratio...@gmail.com on 13 Jan 2012 at 1:25

GoogleCodeExporter commented 9 years ago
This is the code that I've changed according to andre's explanation.

Original comment by eyeratio...@gmail.com on 13 Jan 2012 at 1:27

Attachments:

GoogleCodeExporter commented 9 years ago
THis fixed it for me I had the same error.

Original comment by raz...@cox.net on 27 Jan 2012 at 6:12

GoogleCodeExporter commented 9 years ago
I have the same probleme each time i insert a calendar fith fect, can you help 
me ?

Google.Apis.Calendar.v3.Data.Calendar calendar = new 
Google.Apis.Calendar.v3.Data.Calendar();
        calendar.Summary = NomCalendrier(email);
        calendar.TimeZone = "Europe/Paris";
        calendar.Description = xml.OuterXml;
        calendar.Location = "France";

Original comment by arnaud.c...@gmail.com on 10 Feb 2012 at 9:13